开发者

Insert data / update if already exists

开发者 https://www.devze.com 2023-04-10 17:17 出处:网络
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.

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


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?


MySQL has an INSERT ... ON DUPLICATE KEY UPDATE feature that will let you try to insert a row, or fall back to an update if it discovers a duplicate key (i.e. the row already exists).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号