我使用for循环进行多次插入(4条记录)。然后我想在插入的记录上运行更新查询,其中gateway_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...
}
}
答案 0 :(得分:1)
在每次循环迭代中,将mysql_insert_id()
存储到一个数组中,稍后您可以使用该数组来选择新记录。请注意,我在这里用更整洁的for
替换您的增量foreach
循环:
// Initialize arrays for later
// one for the new ids
$inserted_ids = array();
// one to keep comments from failed queries
$failed_comments = array();
// and one for the MySQL errors of failed queries
$errors = array();
foreach ($pathway_comment as $comment) {
$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);
// If the iteration was successful, save the insert id onto an array
// but only if $pathway_allowed == 'y'
if ($result) {
if ($pathway_allowed == 'y') {
$inserted_ids[] = mysql_insert_id();
}
}
// Otherwise, keep an array of comments that failed
// Maybe useful later if you want to print those that failed
else {
$failed_comments[] = $comment;
// And keep the mysql_error() as well.
$errors[] = mysql_error();
}
}
// Loop is done, now you can implode() the inserted ids array:
// These are all ints from an auto_increment PK, so no additional escaping needed
// Execute the query than do whatever you need with the newly inserted rows.
$query = "SELECT * FROM pathway WHERE pathway_pk IN (" . implode(",", $inserted_ids) . ")";
您之前可能已经听过,但从长远来看,请考虑切换到支持预处理语句的API,例如MySQLi或PDO。你已经完成了所有必要的转义和整数转换,但是如果在循环中编译和执行,预备语句可以更快,除了不需要转义。
答案 1 :(得分:1)
自PHP 5.5.0起,不推荐使用mysql_connect扩展,并将其删除 未来。相反,应该使用MySQLi或PDO_MySQL扩展。 另请参阅MySQL:选择API指南和相关常见问题解答以获取更多信息 信息。此功能的替代方案包括:PDO和mysqli_connect()
如果您使用的是PDO,可以使用它。
$dbh = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
并在for循环中执行以下操作:
$stmt = $dbh->prepare("INSERT INTO test (name, email) VALUES(?,?)");
$tmt->execute( array('user', 'user@example.com'));
$inserted_ids[] = $dbh->lastInsertId();
在循环之后,您将拥有一个包含4个插入ID的数组
编辑:您无需重新选择已输入的值即可使用此
IN FOR LOOP
{
$stmt = $dbh->prepare($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'););
// change the values to bindParam
$stmt->execute( array('user', 'user@example.com'));
$data[$i]['pathway_pk'] = $dbh->lastInsertId();
$data[$i]['case_fk'] = $case_pk;
$data[$i]['level'] = 1;
$data[$i]['pathway_action_fk'] = $action;
$data[$i]['pathway_allowed'] = %s;
$data[$i]['comment'] = $comment;
}
然后
foreach($data as $d){
echo $d['pathway_pk'];
echo $d['case_fk'];
}