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! Link to comment https://forums.phpfreaks.com/topic/42857-solved-grabbing-an-auto-increment-number/ Share on other sites More sharing options...
effigy Posted March 15, 2007 Share Posted March 15, 2007 mysql_insert_id. Link to comment https://forums.phpfreaks.com/topic/42857-solved-grabbing-an-auto-increment-number/#findComment-208085 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... Link to comment https://forums.phpfreaks.com/topic/42857-solved-grabbing-an-auto-increment-number/#findComment-208086 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? Link to comment https://forums.phpfreaks.com/topic/42857-solved-grabbing-an-auto-increment-number/#findComment-208103 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. Link to comment https://forums.phpfreaks.com/topic/42857-solved-grabbing-an-auto-increment-number/#findComment-208108 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. Link to comment https://forums.phpfreaks.com/topic/42857-solved-grabbing-an-auto-increment-number/#findComment-208109 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. ?> Link to comment https://forums.phpfreaks.com/topic/42857-solved-grabbing-an-auto-increment-number/#findComment-208113 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. Link to comment https://forums.phpfreaks.com/topic/42857-solved-grabbing-an-auto-increment-number/#findComment-208114 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.