Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取上次插入记录的ID_Php_Mysqli - Fatal编程技术网

Php 获取上次插入记录的ID

Php 获取上次插入记录的ID,php,mysqli,Php,Mysqli,从我称为“orders”的表中最后插入的记录中检索订单id时遇到问题 一旦检索到,我希望将这些内容放入另一个名为“reports”的表中 我可以看到这个问题已经被问了很多次,我已经提出了很多建议,但都没有成功 目前,我的每个表都包含auto_increment属性,并且我的函数在“orders”表(而不是我试图实现的报告表)上执行成功的插入 functions.php function last_id() { global $connection; return mysqli_inser

从我称为“orders”的表中最后插入的记录中检索订单id时遇到问题

一旦检索到,我希望将这些内容放入另一个名为“reports”的表中

我可以看到这个问题已经被问了很多次,我已经提出了很多建议,但都没有成功

目前,我的每个表都包含auto_increment属性,并且我的函数在“orders”表(而不是我试图实现的报告表)上执行成功的插入

functions.php

function last_id() {

global $connection;

return mysqli_insert_id($connection);



}

在php页面中插入查询后,您可以使用mysqli\u insert\u id($send\u order)。

您应该调用

$last_id = last_id();
在您使用

confirm($send_order); 
试着这样做:

$send_order=query(“插入订单(订单金额、订单交易、订单状态、订单货币)值(“{$amount}”、“{$currency}”、“{$transaction}”、“{$status}”)


$last\u id=$send\u order->insert\u id;

您可以使用
%d
输出最后一个id。 例如:

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

警告!您对SQL注入非常开放,应该使用预先准备好的语句,而不是像那样构建查询。特别是因为您甚至没有以任何方式逃避用户输入。感谢您的指导…为什么不在函数中接受带有链接的参数,而不是使用
global[…]
。所以它会像
last\u id($link)
@Script47您能告诉我如何重构它吗请确保mysqli\u insert\u id($connection);是在insert语句之后生成的,用于检索最后一个id。这有什么帮助?为什么在
确认后它会工作?我不想输出它,您想如何使用最后一个id?我想从我的“订单”表中提取它,并根据上面的代码和问题将它自动插入我的“报告”表中
printf("The last id is:  %d", mysqli_insert_id($connection));