小编典典

相当于mysql_insert_id(); 使用准备好的语句?

sql

我已经使用prepared语句将订单插入到订单表中,但是现在我想根据订单表中插入的最后一个ID将订单项插入到订单表中:

if (isset($_POST['process'])) {

    $order_name = $_POST['order_name'];
    //create initial order
    $stmt = $conn2->prepare("INSERT INTO orders (order_name) VALUES (?)");
    $stmt->bind_param('s', $order_name);
    $stmt->execute();

    //this gets the most recent auto incremented ID from the database - this is the order_id we have just created
    $order_id = mysql_insert_id();

    //loop over all of our order items and add to the database
    foreach ($_SESSION['order'] as $item) {

等同于mysql_insert_id();使用准备好的语句?


阅读 197

收藏
2021-03-10

共1个答案

小编典典

如果您使用的是PDO,则为PDO::lastInsertId()。因此,如果您的数据库句柄称为$link

$order_id = $link->lastInsertId();

如果您使用的是MySQLi,则为mysqli::$insert_idmysqli_insert_id()

$order_id = $link->insert_id;
2021-03-10