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())");

Recommended Answers

All 12 Replies

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

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

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

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

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.

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

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.

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

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

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

so you can dirrectly write a select query.

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.

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.

Ok....that's good.Please mark this thread as Solved..:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.