首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在多次插入后更新特定记录-可能使用last_insert_id()

在多次插入后更新特定记录-可能使用last_insert_id()
EN

Stack Overflow用户
提问于 2013-03-04 10:19:35
回答 2查看 138关注 0票数 2

我有一个使用for循环的多个插入(4条记录)。然后,我想在插入pathway_allowed = 'y‘的记录上运行更新查询(4个记录中只有一个会有这个值)。我猜last_insert_id()在这里会很有用,但是不确定如何使用它来更新插入了以下内容的记录:

代码语言:javascript
复制
$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...
}
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-04 10:27:49

在每次循环迭代中,将mysql_insert_id()存储到一个数组中,以后可以使用该数组来选择新记录。注意,我在这里用一个更整洁的foreach替换了你的增量for循环:

代码语言:javascript
复制
// 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) . ")";

您可能以前听说过这种情况,但从长远来看,请考虑切换到支持预准备语句的应用编程接口,如MySQLi或PDO。在这里,您已经完成了所有必要的转义和整数转换,但是如果在循环中编译和执行预准备语句,除了不需要转义之外,还可以更快。

票数 1
EN

Stack Overflow用户

发布于 2013-03-04 10:40:19

自PHP5.5.0起,

mysql_connect扩展已被弃用,并将在未来删除。相反,应该使用MySQLi或PDO_MySQL扩展。有关更多信息,请参阅MySQL:选择应用程序接口指南和相关常见问题解答。此函数的替代方法包括: PDO和mysqli_connect()

如果你使用的是PDO,你可以使用这个。

代码语言:javascript
复制
$dbh = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');

在for循环中,您可以执行以下操作:

代码语言:javascript
复制
$stmt = $dbh->prepare("INSERT INTO test (name, email) VALUES(?,?)");
$tmt->execute( array('user', 'user@example.com'));
$inserted_ids[] = $dbh->lastInsertId(); 

在循环之后,您将拥有一个包含4个插入的ids的数组

编辑:你不需要重新选择你已经输入的值,你可以使用这个

代码语言:javascript
复制
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;

}

然后

代码语言:javascript
复制
foreach($data as $d){

    echo $d['pathway_pk'];
    echo $d['case_fk'];


}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15193573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档