【发布时间】:2012-03-14 15:51:12
【问题描述】:
对于您的开发人员来说可能是一个简单的
我有这段代码可以将 order_id 和 order_name 插入到“订单”表中:
<?php
// start the session handler
require_once('dbfunction.php');
//connect to database
$conn = DB();
require_once('header.php');
//should we process the order?
if (isset($_POST['process'])) {
$order_name = $_POST['order_name'];
//create initial order
$stmt = $conn2->prepare("INSERT INTO orders (order_name) VALUES (?)");
//bind the parameters
$stmt->bind_param('s', $order_name);
// Execute query
$stmt->execute();
我现在想将订单项插入 order_items 表,但我似乎无法保留插入“orders”表时创建的相同 ID,并将其与 order_items 一起添加到“order_items”表中。这是我的代码:
//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) {
$prod_id = $item['prod_id'];
$quantity = $item['quantity'];
$prod_type = $item['prod_type'];
$stmt = $conn2->prepare("INSERT INTO order_items (order_id, prod_id, quantity, prod_type) VALUES (?, ?, ?, ?)");
//bind the parameters
$stmt->bind_param('iiis', $order_id, $prod_id, $quantity, $prod_type);
// Execute query
$stmt->execute();
}
echo "<p class='black'>Order Processed</p>";
【问题讨论】:
-
带有准备好的语句的sqli
-
看起来不错,有什么问题吗? (问题?)
-
VERSION MYSQLi 如果那是一个版本:s
-
我发现你是这样做的:$order_id = mysqli_insert_id($conn2);谢谢你们的灵感!
标签: php sql prepared-statement shopping-cart