简体   繁体   English

获取最后插入记录的ID

[英]Get ID of Last Inserted Record

I am having an issue retrieving the order_id from the last inserted record in a table I have called 'orders'. 我从我称为“订单”的表中最后插入的记录中检索order_id时遇到问题。

Once retrieved, I would like such to be put into my other table named 'reports'. 一旦检索到,我希望将其放入另一个名为“ reports”的表中。

I can see this question has been asked numerous times and I have instituted as much of the advice without success. 我可以看到这个问题已被问过无数次,并且我已经提出了许多建议,但没有成功。

Currently, each of my tables has the auto_increment attribute included and my function performs a successful INSERT on the 'orders' table (not the reports table however to which I am trying to achieve. 当前,我的每个表都包含auto_increment属性,并且我的函数在“ orders”表(不是报表表)上执行了成功的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 functions.php

function last_id() {

global $connection;

return mysqli_insert_id($connection);



}

您可以在php页面中插入查询后使用mysqli_insert_id($send_order)

You should call 你应该打电话

$last_id = last_id();

after you use 您使用

confirm($send_order); 

Try like this : 尝试这样:

$send_order = query(" INSERT INTO orders (order_amount, order_transaction, order_status, order_currency) VALUES('{$amount}','{$currency}','{$transaction}','{$status}')"); $ send_order = query(“插入订单(order_amount,order_transaction,order_status,order_currency)VALUES('{$ amount}','{$ currency}','{$ transaction}','{$ status}')”) ;

$last_id = $send_order->insert_id; $ last_id = $ send_order-> insert_id;

You can output the last id by using %d . 您可以使用%d输出最后一个ID。 Example: 例:

printf("The last id is:  %d", mysqli_insert_id($connection));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM