master82 Posted February 21, 2007 Share Posted February 21, 2007 I have a simple authentication script, when details are checked a new user is entered into two different tables in the database... mysql_query("INSERT INTO members ('mid', 'memail', 'mcode', 'mpassword', 'msignup', 'mlastip', 'mgender', 'mlaston') VALUES ('', '{$_POST['email']}', '$unique', md5($_POST['password']), UNIX_TIMESTAMP(), '$IP', '{$_POST['gender']}', UNIX_TIMESTAMP())"); mysql_query("INSERT INTO users ('uid', 'uname', 'ugender') VALUES ('NEEDidHERE', '{$_POST['name']}', '{$_POST['gender']}')"); The members table has an auto-incremental setting for 'mid'. My question is how do I edit the code above so that I can insert that unique member id into the unique field of the second table (uid -> NEEDidHERE)? Link to comment https://forums.phpfreaks.com/topic/39466-carrying-over-an-id-to-another-table/ Share on other sites More sharing options...
papaface Posted February 21, 2007 Share Posted February 21, 2007 Not sure if this is the best way to do it, but you could have the first table auto-increment, and then get the ID from that table. Then in the second table dont have an auto-increment and use the same ID in this table as that was generated in the first table. ooo and this is my 300th post Link to comment https://forums.phpfreaks.com/topic/39466-carrying-over-an-id-to-another-table/#findComment-190413 Share on other sites More sharing options...
master82 Posted February 21, 2007 Author Share Posted February 21, 2007 Thats how it is set up... just not sure how to get that id! lol mysql_insert_id() How do I use that? I think that is my answer Link to comment https://forums.phpfreaks.com/topic/39466-carrying-over-an-id-to-another-table/#findComment-190415 Share on other sites More sharing options...
master82 Posted February 21, 2007 Author Share Posted February 21, 2007 If I use... $id = mysql_insert_id() ...after the 1st inser query then use $id in the 2nd insert query. Would that work? Link to comment https://forums.phpfreaks.com/topic/39466-carrying-over-an-id-to-another-table/#findComment-190417 Share on other sites More sharing options...
craygo Posted February 21, 2007 Share Posted February 21, 2007 What you would do is insert the data into the first table, then query the table to get the id of the data you just inserted. <?php mysql_query("INSERT INTO members ('mid', 'memail', 'mcode', 'mpassword', 'msignup', 'mlastip', 'mgender', 'mlaston') VALUES ('', '{$_POST['email']}', '$unique', md5($_POST['password']), UNIX_TIMESTAMP(), '$IP', '{$_POST['gender']}', UNIX_TIMESTAMP())"); $result = mysql_query("SELECT mid FROM members ORDER BY mid DESC LIMIT 1"); $r = mysql_fetch_assoc($result); $uid = $r['mid']; mysql_query("INSERT INTO users ('uid', 'uname', 'ugender') VALUES ('$uid', '{$_POST['name']}', '{$_POST['gender']}')"); ?> Just a question, but why are you holding members and users in 2 different tables?? Why not keep the info in one table and just flag the user as a member with a field. Would make things much easier. Ray Link to comment https://forums.phpfreaks.com/topic/39466-carrying-over-an-id-to-another-table/#findComment-190424 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.