douiwn6941 2013-10-01 03:01
浏览 18
已采纳

如何在连接值上选择最后插入的ID

I'm trying to get the last inserted id of multiple inserted rows.

record_id is auto increment

$sql = "INSERT INTO records (record_id, user_id, status, x) values ";
         $varray = array();

        $rid = $row['record_id'];
        $uid =  $row['user_name'];
        $status =  $row['status'];
        $x =  $row['x'];

        $varray[] = "('$rid', '$uid', '$status', '$x')";

       $sql .= implode(',', $varray);

      mysql_query($sql); 

      $sql2 = "INSERT INTO status_logs (id, record_id, status_id, date, timestamp, notes, user_id, x) VALUES";

      $varray2[] = "(' ', mysql_insert_id(), '$status', '$uid', '$x')";

      $sql2 .= implode(',', $varray2);

       mysql_query($sql2); 

This is the result:

INSERT INTO records (record_id, user_id,  notes, x) values ('', '1237615', 'this is a note', 'active')

INSERT INTO status_logs (log_id, record_id, status_id, date, timestamp, notes, user_id, x) VALUES('', INSERT INTO records (record_id, user_id,  notes, x) values ('', '1237615', 'this is a note', 'active')

INSERT INTO status_logs (log_id, record_id, status_id, date, timestamp, notes, user_id, x) VALUES('', mysql_insert_id(), '1', '2013:05:16 00:00:01', '', this is a note'', '1237615', 'active'), '1', '2013:05:16 00:00:01', '', this is a note'', '1237615', 'active')

There is no value for mysql_insert_id().

  • 写回答

2条回答 默认 最新

  • douying1119 2013-10-01 03:41
    关注

    You're mixing php function mysql_insert_id() and SQL INSERT statement syntax.

    Either use MySQL function LAST_INSERT_ID() in VALUES clause of INSERT statement

    INSERT INTO records (user_id,  notes, x) VALUES('1237615', 'this is a note', 'active');
    INSERT INTO status_logs (record_id, status_id, date, timestamp, notes, user_id, x)
    VALUES(LAST_INSERT_ID(), '1', ...);
          ^^^^^^^^^^^^^^^^^
    

    or retrieve the last inserted id by making a separate call to mysql_insert_id() right after first mysql_query(). And then use that value when you as a parameter to your second query.

    $sql = "INSERT INTO records (user_id, ...) 
            VALUES(...)";
    $result = mysql_query($sql); 
    if (!$result) {
        die('Invalid query: ' . mysql_error()); //TODO beter error handling
    }
    $last_id = mysql_insert_id();
    //         ^^^^^^^^^^^^^^^^^^
    
    $sql2 = "INSERT INTO status_logs (record_id, ...) 
             VALUES $last_id, ...)";
    $result = mysql_query($sql); 
    if (!$result) {
        die('Invalid query: ' . mysql_error()); //TODO beter error handling
    }
    

    Note:

    • You don't need to specify auto_incremented column in column list. Just omit it.
    • Use at least some sort of error handling in your code

    On a side note: Instead of interpolating query strings and leaving it wide open to sql-injections consider to use prepared statements with either mysqli_* or PDO.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程