Jump to content

Carrying over an ID to another table


master82

Recommended Posts

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

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 :D

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.