tags:

views:

208

answers:

4

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

+3  A: 

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;
Jan Hančič
Thank you Jan for your feedback. It does now send the ID but it does not get it in the next page.
francesco
Found the problem. Thank you Jan you saved my day!
francesco
no problem! Glad to help.
Jan Hančič
+1  A: 

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);
Samuel
The auto_increment is successful and connetion is established. All is fine.
francesco
+1  A: 

May be your table has no AUTO_INCREMENT field!

Or. It could be if you use two or more mysql-connections. In this case you should link identifier.

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

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.

Eric Mittler