我有一个使用 for 循环的多个插入(4 条记录)。然后,我想对在 path_allowed = 'y' 中插入的记录运行更新查询(4 个中只有一个具有此值)。我猜 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...
}
}