2

Hi guys I was hoping from some help here, please.

I have a INSERT query to a table, after this is done I am calling:

mysql_insert_id();

In order to send the last ID inserted into the table to the next page like this:

$insertGoTo = "confirm_booking.php?booking_ID=" .$_POST['booking_ID']. "";

Unfortunately it does not work, all I get is a zero.

The table I am inserting into has an auto increment number and values are inserted into it.

I have also tried SELECT MAX(id) FROM mytable. This dosn't work neither.

I know that this problem has been talked about already. I read all posts but nothing came useful.

Many thanks Francesco

2
  • Can you show the exact code you are using? Are you sure you are using the right database connection? Commented Dec 14, 2009 at 12:22
  • Thank you Pekka, the feedback from Jan worked perfectly. PS the database connetion was fine. Commented Dec 14, 2009 at 12:41

9 Answers 9

3

You have to use the value returned by MySql_Insert_Id () when you generate your link:

// your query
$newId = MySql_Insert_Id ();

$insertGoTo = "confirm_booking.php?booking_ID=" . $newId;
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Jan for your feedback. It does now send the ID but it does not get it in the next page.
Found the problem. Thank you Jan you saved my day!
3

Offhand, I can think of a few cases where MySQL wouldn't return the ID:

  • The table you're inserting into doesn't have an AUTO_INCREMENTed primary key.
  • You're inserting multiple rows at once.
  • You're calling mysql_insert_id() from a different connection than the INSERT query was executed.
  • The INSERT query didn't succeed (for instance, it encountered a deadlock). Make sure you are checking the return value from mysql_query(), then use mysql_errno() and mysql_error().

MySQL docs have a full list of conditions and details on how this function works.

Of course, it's also possible there is a bug in MySQL, which would depend on which version of MySQL you are using.

Comments

3

It is possible that your table does not have any AUTO_INCREMENT field!

It could also happen because you have two or more mysql connections at the same time. In this case you should use a link identifier.

$link = mysql_connect( ... );
mysql_select_db('mydb', $link);
mysql_query('INSERT mytable SET abc="123"', $link);
$inserted_id = mysql_insert_id($link);

Comments

2

Some key points from the PHP Manual:

The ID generated for an AUTO_INCREMENT column by the previous query on success, 0 if the previous query does not generate an AUTO_INCREMENT value, or FALSE if no MySQL connection was established.

If not having an AUTO_INCREMENT field is not your problem, you might want to try storing the result of the mysql_query call and using that as an argument to the id function

$result = mysql_query("...");
$id = mysql_insert_id($result);

1 Comment

The auto_increment is successful and connetion is established. All is fine.
1

mysql_insert_id may return 0 or false if your insert fails right? So if you have trouble with mysql_insert_id not retunring what you expect confirm that you don't have a unique constraint or some other problem with your sql that would cause the insert to fail. Using max is a terrible idea if you consider this.

Comments

1

Had an issue using a query like this:

INSERT INTO members (username,password,email) VALUES (...)

  • reason being that the id (which is my primary key and Auto Increment field) is not part of the query.

Changing it to:

INSERT INTO members (id,username,password,email) VALUES ('',...)

using a an empty value '' will have MySQL use the Auto Increment value but also allow you to use it in your query so you can return the insert id

1 Comment

Very useful tip that deserves more upvotes. It is quite common to omit the ID column rather than passing it an empty string when using an auto increment.
0

Make sure to put mysql_insert_id()after the

mysql_query($sql, $con); //Execute the query

Above query responsible for execute your Insert INTO ... command. After you can get the last ID inserted

1 Comment

Question clearly states that mysql_insert_id is issued after mysql_query;
0

I have also suffer from this problem. Finally I found that the problem occur in my connection to the database. You can use this following connection code to connect the database then you can easily use mysqli_insert_id().

$db_connect = mysqli_connect("localhost", "root", "", "social");

Then you can use mysqli_insert_id() as

$id = mysqli_insert_id($db_conx);

I hope this will help you. I you have any problem then leave your comment.

Comments

0

The mysqli_insert_id function has been deprecated. This may be your problem.

Instead, try $mysqli->insert_id. See the documentation for more info.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.