mySql foreign key problems

A

Anonymous

Guest
Hey people,

I have one form that collects user details like name and email address, and one that collects the user's preferences from a list of checkboxes.

When they submit the first form, that data is entered into one table, and the next form is displayed. The checkbox data is stored in another table.

I am having trouble linking the two. There is a userID column in the first table, and I have set that as the Primary Key. There is a UserID column in the second table too, and I have set that up as a Foreign Key that references userID in the first table.

However, when a user fills in both forms, the userID in the first table auto increments, but the userID in the second table just defaults to '0'. I presume this is simply a mySql problem as opposed to a PHP one.

Any help is greatly appreciated. Thanks :help:
 
If you want the id in the second table to match the one in the first, use PHP's mysql_insert_id() to get the id set in the first table and use it when INSERTing into the second table.

I'm not sure but there may also be a MySQL function to accomplish the same thing.
 
hey swirlee,

I can't seem to get that function to work......I think because the last INSERT function that it tries to use is in another php file. Does that make sense?

Also, would that just make the numbers match? I need them to physically be linked together!!

Why does programming make me do this so much?? :shock: :shock: :shock:

Thanks
 
confused_student said:
Also, would that just make the numbers match? I need them to physically be linked together!!

They will never be "physically linked together", because the numbers only exist as 1s and 0s inside your computer. "Physically" doesn't enter into it.

If you want the numbers to match, you have to make them match, by looking at the value of one and setting the value of the other to be the same.

Why does programming make me do this so much??

Because it's not magic. Stop expecting it to be.
 
Back
Top