Jump to content

Recommended Posts

Started to use foreign keys for the first time and have come across a problem

 

I have a table called A_table in which I have a column there which auto-increments named P_ID.

 

I have another table called B_table and have a column called P_ID and i believe I have also set this to foreign key and referenced it to that in A_table.

 

(p_id is the row number)

 

i then have two queries in a page of signup of mine

 


mysqli_query($mysqli, "INSERT INTO A_table (Username, Password, Email, Gender, ip) VALUES ('$euser', '$hash', '$eemail', '$gender', '$ip')");
mysqli_query($mysqli, "INSERT INTO B_table (Energy, Shooting, concentration, brave, health) VALUES (100, 100, 100, 5, 100)");

 

The foreign key is my B_table doesnt have the same id of that in my A_table? It starts at 0, and the query executes and inserts fine

Link to comment
https://forums.phpfreaks.com/topic/272819-foreign-key-issues/
Share on other sites

You can, and you need to if it's a 1-many/0-many etc. But the data you're describing might as well be in one table, it will save you a lot of effort. If you are going to allow every user to have more than one character, they need to be separate. If one user = one character, save the bother and store them in one.

 

Link to comment
https://forums.phpfreaks.com/topic/272819-foreign-key-issues/#findComment-1404068
Share on other sites

There are valid cases for having two tables with a one-to-one relationship.

 

Consider a employee table. It makes sense to put personal details (salary, tax etc) in one table and public stuff (name, job title, extension number etc) in another. That way permissions to access the two sets of data can be different.

Link to comment
https://forums.phpfreaks.com/topic/272819-foreign-key-issues/#findComment-1404077
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.