I'm trying to make a website that allows members to register. I've got a database set up with multiple tables. Simplified it looks like the following:
Users has two columns: 'ID' and 'Username', where 'ID' is the primary key.
Messages has two columns: 'ID' and 'Message', where 'ID' is a foreign key that refers to Users.
UserRank has several columns as well, including an 'ID' that is also a foreign key to Users.
There's even more tables that use the same 'ID' to identify the user/member.
I use MySQL to send and store the data from the webpage to the database like this:
$sqlQuery = "INSERT INTO users (Username) VALUES
('$username')";
$result=mysql_query($sqlQuery);
Users gets filled like it should, but the 'ID' field of Messages and UserRank remains empty. I want to fill them accordingly, i.e. for every user in Users (who all have an unique ID due to it being a primary key), there should be a record in the other tables as well.
I've tried to fill them manually with other queries but this doesn't seem to work:
$idQuery = mysql_query("SELECT ID FROM users WHERE Username='$username'");
$result = mysql_fetch_array( $idQuery );
$ID=$result['ID'];
$query= "INSERT INTO Messages (ID) VALUES ('$ID')";
$result=mysql_query($query);
I know it's a weird construction but I had to try something. Please point me in the right direction here.