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.”); }
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.
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.
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:
I have also got some general comments about your code snippet too:
isset() is a variadic, and as such you can pass multiple arguments to it.
It would be best to validate other input fields (especially those with specific formats, like the phone field).
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.
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().
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.
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.