Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8662807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:49:18+00:00 2026-06-12T16:49:18+00:00

I’ve always found myself creating two separate php files/scripts for adding a certain data

  • 0

I’ve always found myself creating two separate php files/scripts for adding a certain data and editing this data. These files weren’t that much different, so I figured there should be a way how to make them into one file.

Here I’ll present a very simple example to illustrate my point:

add.php:

<?php

$title = $_POST['title']; // ignore the unescaped data, this is a simple example
$text = $_POST['text'];
mysqli_query($connection,
   "INSERT INTO `articles` (`title`, `text`) VALUES ('$title', '$text')");

echo'
<form>
   <input type="text" name="title" value="'.$_POST['title'].'" />
   <input type="text" name="text" value="'.$_POST['text'].'" />
   <input type="submit" value="Add" />
</form>
';

?>

edit.php:

<?php

$id = $_GET['id'];
$title = $_POST['title']; // ignore the unescaped data, this is a simple example
$text = $_POST['text'];
// save data
mysqli_query($connection,
   "UPDATE `articles` SET `title` = '$title', `text` = '$text'
    WHERE `id` = $id");

// get current data
$q = mysqli_query($connection,"SELECT * FROM `articles` WHERE `id` = $id");
$d = mysqli_fetch_array($q);
$title = $d['title'];
$text = $d['text'];

echo'
<form>
   <input type="text" name="title" value="'.$title.'" />
   <input type="text" name="text" value="'.$text.'" />
   <input type="submit" value="Add" />
</form>
';

?>

As you can see, the add and edit forms/codes are very similar, except that:

  • add inserts the data, while edit updates it
  • add inserts $_POST values into the form (if there’s an error, so that the submitted data remains in the form, while edit inserts the current database values into the form (after the save is complete and the page refreshes, so that the form has the current db values)

Can these two somehow be merged into one file/code, so that if I want to add/change the form values, I don’t need to edit two files separately, but will change the form only once?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:49:20+00:00Added an answer on June 12, 2026 at 4:49 pm

    You can use a INSERT ON DUPLICATE KEY UPDATE which roughly gave you :

    <?php
    $id = $_GET['id'];
    $title = $text = '';
    
    if ($_POST)
    {
        $title = $_POST['title'];
        $text = $_POST['text'];
        // save data
        $query = "INSERT INTO `articles` (`id`, `title`, `text`)
                  VALUES ('$id', '$title', '$text')
                  ON DUPLICATE KEYS UPDATE title = title, text = text"
        mysqli_query($connection, $query);
    
    }
    else if ($id)
    {
        // get current data
        $q = mysqli_query($connection, "SELECT * FROM `articles` WHERE `id` = $id");
        $d = mysqli_fetch_array($q);
        $title = $d['title'];
        $text = $d['text'];
    }
    
    echo '
    <form>
       <input type="text" name="title" value="'.$title.'" />
       <input type="text" name="text" value="'.$text.'" />
       <input type="submit" value="Add" />
    </form>';
    
    • If it’s a POST and no $id present : a new row is inserted just like an INSERT.
    • If it’s a POST and an $id is present : if $id already exist in the table than the row is updated otherwise it’s an INSERT.
    • If you only have an $id : show the form with existing data in it.
    • If it’s not a POST and $id isn’t populated : show an empty form.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.