我已经使用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();使用准备好的语句?
mysql_insert_id();
如果您使用的是PDO,则为PDO::lastInsertId()。因此,如果您的数据库句柄称为$link:
PDO::lastInsertId()
$link
$order_id = $link->lastInsertId();
如果您使用的是MySQLi,则为mysqli::$insert_id或mysqli_insert_id():
mysqli::$insert_id
mysqli_insert_id()
$order_id = $link->insert_id;