我有一個使用for循環的多次插入(4條記錄)。然後,我想在插入path_allowed ='y'的記錄上運行更新查詢(只有四個中的一個具有此值)。我猜LAST_INSERT_ID()在這裏很有用,但不知道如何使用它來更新插入該記錄如下:多次插入後更新特定記錄 - 可能使用last_insert_id()
$pathway_allowed = intval($_POST['allowed']);
$action = mysql_real_escape_string($_POST['actions']);
if(isset($_POST['submit'])){
$pathway_comment = array();
foreach($_POST['comment'] as $comment) {
$pathway_comment[]= mysql_real_escape_string($comment);
}
for($i=0, $count = count($pathway_comment);$i<$count;$i++) {
$comment = $pathway_comment[$i];
$query = sprintf(
"INSERT INTO pathway (
pathway_pk,
case_fk,
level,
pathway_action_fk,
pathway_allowed,
comment
) VALUES (
'',
'$case_pk',
'1',
'$action',
'%s',
'$comment')", $pathway_allowed === $i ? 'y' : 'n');
$result = mysql_query($query, $connection) or die(mysql_error());
}
if($result){
- SELECT the 4 records here...
}
}
彼得,你應該知道'mysql_query'將來消失你知道嗎?在我的回答中,我已經包含了'PDO',因爲如果PHP 5.5出現了,你將會替換你的代碼。現在它已經發布到'PHP 5.5.0 Alpha5中了。如果你的主機有這個PHP版本,你的代碼將不會被執行。只是讓你知道我的朋友:) – Othman 2013-03-04 04:58:07