acmumph Posted December 5, 2010 Share Posted December 5, 2010 Trying to utilize the LAST_INSERT_ID() feature but can't seem to get it right in my code: //Get data in local variable $first=$_POST['first']; $last=$_POST['last']; $street=$_POST['street']; $city=$_POST['city']; $zip=$_POST['zip']; $phone=$_POST['phone']; $position=$_POST['position']; $committee=$_POST['committee']; $species=$_POST['species']; $breed=$_POST['breed']; $dob=$_POST['dob']; $tag=$_POST['tag']; $weight=$_POST['weight']; $query="insert into members(id, first, last, street, city, zip, phone,position,committee) values(NULL,'$first','$last','$street','$city','$zip','$position','$committee')"; $query1="insert into animal(animal_id, exhibitor_id, animal_species, animal_breed, animal_dob, animal_tag, animal_weight) values(NULL, LAST_INSERT_ID(),'$species','$breed','$dob','$tag','$weight')"; mysql_query($query) or die(mysql_error()); mysql_query($query1) or die(mysql_error()); header("Location: members.php"); //echo "Your message has been received"; If I do the following from MySQL prompt it works: INSERT INTO members(id, first, last) VALUES (NULL, "Test", "Me"); INSERT INTO animal(animal_id, exhibitor_id,animal_species) VALUES (NULL, LAST_INSERT_ID(),"Heifer"); Appreciate your time Quote Link to comment https://forums.phpfreaks.com/topic/220747-last_insert_id/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 5, 2010 Share Posted December 5, 2010 You are not even trying the same queries at the mysql prompt that you are running in your php code. At least your first query is failing with an sql error because the number of fields and the number of data values don't match. Your php code should have error checking logic (check if a query worked or failed), error reporting/logging logic (output a user error message and log system level information about the error), and error recovery logic (what execution path do you take when there is an error, attempt to use missing data and generate more errors or skip over dependent code?) Quote Link to comment https://forums.phpfreaks.com/topic/220747-last_insert_id/#findComment-1143251 Share on other sites More sharing options...
acmumph Posted December 5, 2010 Author Share Posted December 5, 2010 Thanks for the insight...I managed to get it working with following code: //Get data in local variable $first=$_POST['first']; $last=$_POST['last']; $street=$_POST['street']; $city=$_POST['city']; $zip=$_POST['zip']; $position=$_POST['position']; $committee=$_POST['committee']; $species=$_POST['animal_species']; $breed=$_POST['animal_breed']; $dob=$_POST['animal_dob']; $tag=$_POST['animal_tag']; $weight=$_POST['animal_weight']; // check for null values $query="insert into members(id, first, last, street, city, zip, position,committee) values(NULL,'$first','$last','$street','$city','$zip','$position','$committee')"; $query1="insert into animal(animal_id, exhibitor_id, animal_species, animal_breed, animal_dob, animal_tag, animal_weight) values(NULL, LAST_INSERT_ID(),'$species','$breed','$dob','$tag','$weight')"; mysql_query($query) or die(mysql_error()); mysql_query($query1) or die(mysql_error()); header("Location: members.php"); //echo "Your message has been received"; I'm relatively new at this and trying to learn as I go. I know just enough to hurt myself at this point but will be sure to think of how and where to add error checking. Thanks for the help!! Quote Link to comment https://forums.phpfreaks.com/topic/220747-last_insert_id/#findComment-1143257 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.