我遇到一个问题,就是在我已调用' orders'的表格中从最后插入的记录中检索order_id
。
一旦检索到,我希望将其放入名为' reports'的其他表中。
我可以看到这个问题被多次提出,我已经提出了很多建议而没有成功。
目前,我的每个表都包含auto_increment属性,并且我的函数在'命令'上执行成功的INSERT。表(不是我想要实现的报告表。
function report() {
if(isset($_GET['tx'])) {
$amount = $_GET['amt'];
$currency = $_GET['cc'];
$transaction = $_GET['tx'];
$status = $_GET['st'];
$total = 0;
$item_quantity = 0;
$send_order = query(" INSERT INTO orders (order_amount, order_transaction, order_status, order_currency) VALUES('{$amount}','{$currency}','{$transaction}','{$status}')");
$last_id = last_id(); // this calls last_id in functions.php
confirm($send_order);
foreach ($_SESSION as $name => $value) {
if($value > 0 ) {
if(substr($name, 0, 8) == "product_") {
$length = strlen($name - 8);
$id = substr($name, 8 , $length);
$query = query(" SELECT * FROM products WHERE product_id = " . escape_string($id). " " );
confirm($query);
while ($row = fetch_array($query)) {
$product_price = $row['product_price'];
$sub = $row['product_price']*$value;
$item_quantity +=$value;
$insert_report = query(" INSERT INTO reports (product_id, order_id, product_price, product_quantity) VALUES('{$id}','{$last_id}','{$product_price}','{$value}')");
confirm($insert_report); //runs the confirm helper method
} // end of while loop
$total += $sub;
echo $item_quantity;
} // end of substring if statement
}// end of if value
} // end of foreach loop
session_destroy();
} else {
redirect("index.php");
} //end of if isset GET tx
} // end of report function /////////////////
的functions.php
function last_id() {
global $connection;
return mysqli_insert_id($connection);
}
答案 0 :(得分:0)
您可以在php页面中插入查询后使用mysqli_insert_id($send_order)
。
答案 1 :(得分:0)
你应该致电
$last_id = last_id();
使用之后
confirm($send_order);
答案 2 :(得分:0)
试试这样:
$ send_order = query(“INSERT INTO orders(order_amount,order_transaction,order_status,order_currency)VALUES('{$ amount}','{$ currency}','{$ transaction}','{$ status}' )“);
$ last_id = $ send_order-> insert_id;
答案 3 :(得分:0)
您可以使用%d
输出最后一个ID。
例如:
printf("The last id is: %d", mysqli_insert_id($connection));