Jump to content

LAST_INSERT_ID()


acmumph

Recommended Posts

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

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.