English 中文(简体)
如果已经存在,则列入数据/最新情况
原标题:Insert data / update if already exists
  • 时间:2011-10-04 06:17:38
  •  标签:
  • php
  • mysql

该法典行之有效。 我可以说明如何将数据插入db 如果用户首次使用“SAVE”纽吨或更新数据。

www.un.org/Depts/DGACM/index_spanish.htm php一侧

<?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

由于@jmlsteeke i 找到了途径

将这部法典放在html部分

<?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);  

?>

添加以下文字:

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

使用的服务器代码

$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);

感谢@jmlsteeke

最佳回答

一种常见的做法是,在你编辑该页时,把id作为隐蔽的场所。 当用户提交该页时,如果在场的话,你就会发布统一民主党的指挥,如果在场的话,你会知道这个新页,并发布INSERT的指挥。

如果你需要我更透彻的话,让我知道。

Edit: 更富裕

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
}

请允许我说,有一个叫作表.php的网址,负责显示表格,另一个称为DoForm.php的网址,负责处理表格。

如果用户访问表.php没有指定身份(http://example.com/Form.php),就会显示以下表格:

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

用户将添加一些信息,点击提交纽特和多Form,将获得以下持久性有机污染物:

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

表格将检查是否存在<代码>_POST[id]。 性别问题专家咨询组发布新页。

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

表格. .php 参看id,其印刷形式如下:

<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>

用户固定其类型,将网络空间改为网页,并提交点击。 • 收到以下员额变量

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

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

更加清楚吗?

问题回答

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).





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签