Php not sending mysql_insert_id back

Hi,

I have the following form with ajax submission. Everything works except the php not returning the mysql id of the inserted record. I have checked myself and the record is adding fine in the database, just that the mysql_insert_id() is not returning the id number as it should be back to the ajax.

[code]<?php
require_once (“config.php”);

if (isset($_POST[“name”]) and isset($_POST[“email”]))
{
if (empty($_POST[“name”])) { die(“Please specify your Name.”); }
if(!filter_var($_POST[“email”], FILTER_VALIDATE_EMAIL)) { die (“Please specify a valid e-Mail Address.”); }

$q0 = "INSERT INTO enquiries SET
	enqr_name = " . fix($_POST["name"]) . ",
	enqr_email = " . fix($_POST["email"]) . ",
	enqr_phone = " . fix($_POST["phone"]) . ",
	enqr_message = " . fix($_POST["message"]) . ",
	enqr_ipaddress = " . fix($_SERVER["REMOTE_ADDR"]) . ",
	enqr_timedate = " . fix(time());
$r0 = mysql_query($q0) or die("Query Failed: Add Enquiry");
$id = mysql_insert_id();

die($id);

}
?>

Name: " />
Email: " />
Telephone: " />
Tell us a little about what you need:
<?=$_POST["message"]?>

[/code]

Any help ?

Thanks.

How is the enquiries table defined? Do you actually have an autoincrement field in the table definition that can be returned?

Is there some particular reason why you need such a field anyway - since either the time by itself or the time and email together should produce a suitable unique key (since the same person can’t make two enquiries at exactly the same time).

Also if the PHP call to get the id isn’t working you could try substituting the equivalent SELECT call in mySQL and see if it works that way.

Standard disclaimer: the mysql_ library is deprecated as of PHP 5.5, and will be removed in the future. Suggest upgrading to mysqli or PDO.

As for why you’re not getting back what you think you should, i think this note in the PHP documentation for die (exit()) is the root of your problem…

Note: PHP >= 4.2.0 does NOT print the status if it is an integer.

A inserted ID would be an integer return. So nothing gets printed.
Try echoing the ID before you die.

Why would you add die($id)? I would say get rid of that line.

Because he wants to ajax-call this same page to process the form; not exactly how i’d handle it, but its doable.

Any way why do one need ajax to handle data just use PHP and can get similar effect using headers and from my point I do have agree with others remove the die($id) will solve the issue or get rid of the ajax will ease the coding more.

If you echo $q0 what do you get?

Your $q0 query should fail, causing the ‘Query Failed: Add Enquiry’ sentence to be output. The reason why your query is set to fail is because you aren’t encasing the string values within the query inside quotes (to delimit them). You should therefore change your $q0 variable to the following:

$q0 = "INSERT INTO enquiries SET
		enqr_name = '" . fix($_POST["name"]) . "',
		enqr_email = '" . fix($_POST["email"]) . "',
		enqr_phone = '" . fix($_POST["phone"]) . "',
		enqr_message = '" . fix($_POST["message"]) . "',
		enqr_ipaddress = '" . fix($_SERVER["REMOTE_ADDR"]) . "',
		enqr_timedate = " . fix(time());

I have also got some general comments about your code snippet too:

  1. isset() is a variadic, and as such you can pass multiple arguments to it.
  2. It would be best to validate other input fields (especially those with specific formats, like the phone field).
  3. Give your variables more meaningful names to convey what they do. For example, changing your $q0 variable to $enquiriesInsert is a lot better from a readability point of view.
  4. I’m not quite sure what your fix() function does, but I can’t see it being much use sanitising the return value from time().
  5. Even with the limited functionality the original MySQL extension provides, it still has the mysql_error() function that shows the error if one has occurred. You will find it very useful when debugging (though again, as said above, using a newer extension would bee better).

Don’t use die() to output data - its argument is status and is printed only if it is a string. Use echo and then die() without arguments if you need to terminate the script. From the manual:

   If status is a string, this function prints the
   status just before exiting.
  
  
   If status is an integer, that
   value will be used as the exit status and not printed. Exit statuses should be in
   the range 0 to 254, the exit status 255 is reserved by PHP and shall
   not be used. The status 0 is used to terminate the program
   successfully.

Hi,

Yes here’s a enqr_id which has auto increment.

Thanks.

Hi,

I guess that was the issue. As I was doing a integer in die. I will try echo and then exit().

Thanks.

Hi,

Yes that is what I am trying to do.

Thanks.

Hi,

The fix function is is encasing the string values. Hence the query is not failing. It also filters any html tags or other stuff. I use mysql_error when I am making, but when I upload etc. I change it to custom messages, so incase a query fails for some reason a hacker etc. cannot see the col. names etc. Just a precaution on my end.

@Lemon_Juice Thanks. I guess that was causing the issue. I will do echo now then exit.

Thanks all.