aebstract Posted March 15, 2007 Share Posted March 15, 2007 Hello, I am entering information in to a database. Everything enters 100% fine, the first column is an auto_increment id number. What I want to do is enter information in to another table, but I need to use that id number that is generated in one of the columns. (and it has to be pretty much instant with the first one, its for a register form) Is this possible? If so, how would I do it? Thanks! Quote Link to comment Share on other sites More sharing options...
effigy Posted March 15, 2007 Share Posted March 15, 2007 mysql_insert_id. Quote Link to comment Share on other sites More sharing options...
Orio Posted March 15, 2007 Share Posted March 15, 2007 mysql_insert_id() Orio. EDIT- I am getting slow... Quote Link to comment Share on other sites More sharing options...
aebstract Posted March 15, 2007 Author Share Posted March 15, 2007 $result = mysql_query("INSERT INTO users (firstname,midname,lastname,email,password,date_signup,ip_signup,country,address1,address2,city,state,postal,telephone)". "VALUES ('$firstname', '$midname', '$lastname', '$email', '$password', 'now()', '$ipsignup', '$country', '$address1', '$address2', '$city', '$state', '$postal', '$telephone')") or DIE(mysql_error()); $result2 = mysql_query("INSERT INTO map_users_roles (users_id,roles_id)". "VALUES ('mysql_insert_id()', '3')") or DIE(mysql_error()); Okay, with that function in, it is placing '0' in the column. I also tryed mysql_insert_id(users_id) thinking that maybe I had to put the column name in place? What am I doing wrong here? Quote Link to comment Share on other sites More sharing options...
Orio Posted March 15, 2007 Share Posted March 15, 2007 I think it's trying to place the string "mysql_insert_id()" into the int column so it enters 0. Try this: $result2 = mysql_query("INSERT INTO map_users_roles (users_id,roles_id)". "VALUES ('".mysql_insert_id()."', '3')") or DIE(mysql_error()); Orio. Quote Link to comment Share on other sites More sharing options...
per1os Posted March 15, 2007 Share Posted March 15, 2007 It should just be mysql_insert_id($result); or just a blank modifier to get the last result's id. Quote Link to comment Share on other sites More sharing options...
obsidian Posted March 15, 2007 Share Posted March 15, 2007 I think you may have the usage of the function backwards. Can you describe what you're trying to do? mysql_insert_id() returns the auto_incremented value of a newly inserted row, it doesn't tell you what the next ID is supposed to be: <?php $sql = mysql_query("INSERT INTO myTable (name) VALUES ('$val')"); $id = mysql_insert_id(); // returns the ID generated in the above query. ?> Quote Link to comment Share on other sites More sharing options...
aebstract Posted March 15, 2007 Author Share Posted March 15, 2007 Thanks, both of you, for the information and all. I did what orio said with the ." ". and it helped, I wasn't even thinking about that. Quote Link to comment 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.