frapet
frapet

Reputation: 229

php mysql_insert_id(); not working

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

Upvotes: 2

Views: 17329

Answers (8)

Boycott A.I.
Boycott A.I.

Reputation: 18971

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

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

Upvotes: 0

santosh devnath
santosh devnath

Reputation: 172

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.

Upvotes: 0

Denuwan.hh
Denuwan.hh

Reputation: 66

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

Upvotes: 0

WebsterDevelopine
WebsterDevelopine

Reputation: 117

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

Upvotes: 2

merkushin
merkushin

Reputation: 481

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);

Upvotes: 3

Eric Mittler
Eric Mittler

Reputation: 11

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.

Upvotes: 1

Samuel
Samuel

Reputation: 17171

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);

Upvotes: 2

Jan Hančič
Jan Hančič

Reputation: 53929

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;

Upvotes: 3

Related Questions