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 6670687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:17:54+00:00 2026-05-26T03:17:54+00:00

This code works. I can’t figure out how to insert data into db If

  • 0

This code works. I can’t figure out how to insert data into db If user pressed “SAVE” button for the first time or update data.

The php side

<?php
require '../../core/includes/common.php';

$name=filter($_POST['name'], $db);
$title=filter($_POST['title'], $db);
$parentcheck=filter($_POST['parentcheck'],$db);
if(isset ($_POST['parent'])) $parent=filter($_POST['parent'],$db);
else $parent=$parentcheck;  
$menu=filter($_POST['menu'], $db);
$content = $db->escape_string($_POST['content']);

$result=$db->query("INSERT INTO menu (parent, name, showinmenu) VALUES ('$parent', '$name', '$menu')") or die($db->error);
$new_id = $db->insert_id;
$result2=$db->query("INSERT INTO pages (id, title, content) VALUES ('$new_id', '$title', '$content')") or die($db->error);  

if ($new_id>0){  
echo "{";  
echo '"msg": "success" ';
echo "}";  
}else{ 
echo "{"; 
echo
'"err": "error"';  
echo "}";  
}

?>

UPDATE

Thanks to @jmlsteeke i found the way

Place this piece of code in html part

<?php
$result=$db->query("INSERT INTO menu (parent, name, showinmenu) VALUES ('555', 'new', '0')") or die($db->error);
$new_id = $db->insert_id;
$result2=$db->query("INSERT INTO pages (id, title, content) VALUES ('$new_id', 'new', 'new')") or die($db->error);  

?>

And added following code into form

<input type="hidden" name="id" value="<?=$new_id?>"/>

In serverside script used

$result=$db->query("UPDATE pages AS p, menu AS m SET m.parent='$parent', m.name='$name', m.showinmenu='$menu', p.id='$id', p.title='$title', p.content='$content' WHERE m.id='$id' AND p.id=m.id") or die($db->error);

Thank you @jmlsteeke

  • 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-05-26T03:17:55+00:00Added an answer on May 26, 2026 at 3:17 am

    A common way would be to store the id as a hidden field when you are editing the page. This way when the user submits the page, if there is an id present, you issue the UPDATE commands, and if there isn’t one present, you know it’s a new page, and issue the INSERT commands.

    If you need me to be more thorough let me know.

    Edit: Being More Thorough

    I’ll make a simple, complete, example of what I mean.

    Form.php pseudo code

    //set default values for fields
    //print form tag
    if (isset($'id',$_GET)) {
        //fetch data from database
        //print hidden id field
        //override default values for fields
    }
    //print rest of fields using default values (possibly overridden)
    

    DoForm.php pseudo code

    //Sanitize user input
    if (isset('id',$_POST)) {
        //UPDATE database with user input
    } else {
        //INSERT new rows into table with user input
    }
    

    Let’s say you have a php file called Form.php which is responsible for displaying the form, and another php script called DoForm.php which is responsible for handling the form.

    If a user visits Form.php with no ID specified (http://example.com/Form.php) then it will display the following form:

    <form method="post" action="DoForm.php">
    <input type="text" name="name" value="" />
    <input type="text" name="title" value="" />
    ... other stuff ...
    </form>
    

    The user will add some information, click on the submit button and DoForm will get the following POST variables:

    "name"  => "NewPageName"
    "title" => "My First Webpag" [intetional typo, see later]
    ... other stuff ...
    

    DoForm will check to see if $_POST['id'] exists. Since it doesn’t DoForm issues the INSERT commands to add a new page.

    Later on, the user realises the made a typo, and goes to fix it. The user clicks on the “Edit Page” control for “NewPageName” which will be http://example.com/Form.php?id=1

    Form.php see’s that id is set, so the form it prints out is as follows:

    <form method="post" action="DoForm.php">
    <input type="hidden" name="id" value="1"
    <input type="text" name="name" value="NewPageName" />
    <input type="text" name="title" value="My First Webpag" />
    ... other stuff ...
    </form>
    

    The user fixes their type, changing Webpag to Webpage, and hits submit. DoForm gets the following Post variables

    "id"    => 1
    "name"  => "NewPageName"
    "title" => "My First Webpage"
    ... other stuff ...
    

    DoForm sees that id is set, and so uses UPDATE instead of INSERT.

    Any more clear?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I cannot figure out why this code works locally on my PC (localhost) but
I'm getting really tired of trying to figure out why this code works in
This code works, and I can see the request go out to my php
how can I insert on duplicate key in this Code to remove duplicate words?
This code works, but is inefficient because it double-lookups the ignored dictionary. How can
To clarify: no I can't make this pure PHP5 yes this code works in
I can’t seem to wrap my head around the first part of this code
Can someone please explain in plain language how this code works to give a
i have a problem with jquery/visible. Hope somebody can help me. This code works,
I have this code i am doing for university. The first code works as

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.