Reputation: 1088
How to get the auto incremented field after inserting to the database example. If a complaint is registered and the complaint id should be shown to the user.
$sql="INSERT INTO $tbl_name (cardno, comp, firno, compdate) VALUES ('$CardNo', '$Reason', '$Fir',CURDATE())";
mysql_query($sql);
I tried this but its showing only the 1st inserted record.
Upvotes: 0
Views: 466
Reputation: 193251
It's also worth mentioning that mysql_insert_id()
(for mysql extension) and $mysqli->insert_id (for mysqli) are the wrappers for native MySQL function LAST_INSERT_ID
. So you can also use it in this way:
SELECT LAST_INSERT_ID(); // select last generated auto increment id
Upvotes: 1
Reputation: 212402
mysql_insert_id() returns the autoincrement id from the last query
But forget mysql, move into the 20th century and use mysqli (or even into the 21st century and use PDO)
Upvotes: 4