mysql_insert_id() 查看最后插入记录的值
我有一个 PHP 脚本,可以将数据插入 mysql 数据库。该表有一个自动增量列。
我想使用 mysql_insert_id() 函数来获取最后插入记录的值。
由于某种原因,这会返回值 0,其中 autoid 列的值为 300037。
如有任何帮助,我们将不胜感激。谢谢。
我的代码片段如下:
$sqlinsertdelivery="INSERT INTO deliverydetails (`LoadID1`,`deliveryroute`,`source`,`destination`,`route`,`CollectionArea1`, `CollectionArea2`, `CollectionArea3`, `CollectionArea4`, `CollectionDate`, `collectiontime`, `Destination1`, `Destination2`, `Destination3`, `Destination4`, `ArrivalDate`, `ArrivalTime`, `DeliveryCreationDateTime`, `DeliveryCreatedBy`,`comments`,`trailertype`)
VALUES
($loadnumber,'$route1','Hulamin','$dest1','$_SESSION[sessionrouteid]','$_POST[dispatchid1]','$_POST[dispatchid2]','$_POST[dispatchid3]','$_POST[dispatchid4]','$_POST[coldatetime]','$_POST[deltime]','$_POST[drop1]','$_POST[drop2]','$_POST[drop3]','$_POST[drop4]','$_POST[arrivedatetime]','$_POST[arrivetime]',CURRENT_TIMESTAMP,'$session->username','$_POST[comments]','$_POST[vehicletype]')
";
if (!mysql_query($sqlinsertdelivery,$con))
{
die('Error with insert statement: ' . mysql_error());
}
$delid = mysql_insert_id();
echo $delid;
I have a PHP script that inserts data into a mysql database. The table has an auto increment column.
I want to use the mysql_insert_id() function to get teh value of the last inserted record.
For some reason this is returning the value 0 where the autoid column has a value of 300037.
Any assistance is appreciated. Thank you.
my code snippet is below:
$sqlinsertdelivery="INSERT INTO deliverydetails (`LoadID1`,`deliveryroute`,`source`,`destination`,`route`,`CollectionArea1`, `CollectionArea2`, `CollectionArea3`, `CollectionArea4`, `CollectionDate`, `collectiontime`, `Destination1`, `Destination2`, `Destination3`, `Destination4`, `ArrivalDate`, `ArrivalTime`, `DeliveryCreationDateTime`, `DeliveryCreatedBy`,`comments`,`trailertype`)
VALUES
($loadnumber,'$route1','Hulamin','$dest1','$_SESSION[sessionrouteid]','$_POST[dispatchid1]','$_POST[dispatchid2]','$_POST[dispatchid3]','$_POST[dispatchid4]','$_POST[coldatetime]','$_POST[deltime]','$_POST[drop1]','$_POST[drop2]','$_POST[drop3]','$_POST[drop4]','$_POST[arrivedatetime]','$_POST[arrivetime]',CURRENT_TIMESTAMP,'$session->username','$_POST[comments]','$_POST[vehicletype]')
";
if (!mysql_query($sqlinsertdelivery,$con))
{
die('Error with insert statement: ' . mysql_error());
}
$delid = mysql_insert_id();
echo $delid;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能性不大,但首先尝试用
mysql_insert_id($con);
替换mysql_insert_id();
顺便说一句,请告诉我这是不会<的代码/strong> 正在生产中。
Long shot, but first try replacing
mysql_insert_id();
withmysql_insert_id($con);
On a side note, PLEASE tell me this is code that will not be in production.