0

I've been trying for hours to retrieve an ID from a previous insert statement to include in the next insert statement. I am not sure what I am doing on but a new fresh set of eyes can see the issue better than I can at the moment. Any help is greatly appreciated:

//run the query which adds the data gathered from the form into the database
	$result = $chmdb->query("INSERT INTO " . DB . "members (user_id,username, first_name, last_name, email, address1, address2, city, state, zip, 
	phone, member, password, active) VALUES ('NULL','$username','$first_name','$last_name','$email','$address1','$address2','$city','$state',
	'$zip','$phone','$member','$password','1')");
	
	$uID = $chmdb->query("SELECT LAST_INSERT_ID() FROM " . DB . "members");
	
	$chmdb->query("INSERT INTO " . DB . "user_roles (userID,roleID,addDate) VALUES ('$chmdb->uID','$roleID',NOW())");

Edited by joshmac: updated first query statement

2
Contributors
12
Replies
13
Views
5 Years
Discussion Span
Last Post by pro_learner
0

In the first query,from where did you get the value 'LAST_INSERT_ID()'?

That shouldn't be there, it should be '' or NULL.

0

But you are inserting the value of 'LAST_INSERT_ID()' to the database..

I am trying to get the incremental id from the $result query and insert it into the $uID query.

0

Ok....i can see you have corrected it in your query.So which query did you execute first?First you should execute uID query then execute the rsult query..

0

Ok....i can see you have corrected it in your query.So which query did you execute first?First you should execute uID query then execute the rsult query..

I need the ID from the $result query.

0

Did you have put ID as an auto incremented number in your Database?

Yes, user_id in the DB . members table is auto_increment.

0

so you can dirrectly write a select query.

I did, the SELECT query is $uID and I am trying to use that to get the ID to insert into the 3rd query.

0

I did, the SELECT query is $uID and I am trying to use that to get the ID to insert into the 3rd query.

Thanks for your help, but I figured it out.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.