+ 2
Php mysqli
Is it necessary to join two tables in which one has a foreign key before it’ll be possible to insert data from different pages into the tables?
31 ответ
0
usualy we join two tables for the purpuse of view, and the insert statement work on a table or a simple view from one table, not on a view from multiple tables
0
the insert works when the foreign key isnt set on the child table. i dont know why
0
the child table have to contain the foreign key, and also your insert statement on the child table must contain the foreign key in order to work
0
i tried that but didnt work. could there be another reason?
0
sure that could be another reason
0
could it be that because they’re on different pages? the values don’t insert on the page with the foreign key table
0
send me your two tables structure and your insert request, and i will have a look later
0
ok
0
$sql=“INSERT INTO users (username, fname)VALUES(‘$username’, ‘$fname’);
mysqli_query($link, $sql);
on the child insert on the other page:
$sql=“INSERT INTO relationship (user_id, relationship_type)VALUES(‘$user_id’, ‘$relationship_type’);
mysqli_query($link, $sql);
table structure relationship
relationship_id int(11) primary key
relationship_type varchar(255)
user_id foreign key reference (users)
0
you think i should include the mysqli_insert_id() ?
0
to find if your query contain to right values before the execution, try to print it on the screen and see if every value is correct
0
how do you mean?
0
you can use mysqli_insert_id() if the INSERT INTO users is the last query before the INSERT INTO relationship query
0
echo $sql;
0
the user-id which is the foreign key only prints out as zero as the value
0
sorry for the late answer, you have to assign the right value to $user-id before using it in the query
0
i did assign it by using the mysqli_inser_id(), but still didnt work
0
did you verify the query with echo $sql after using the mysqli_insert_id
0
yeah, and the value for the userid was ‘0’
0
so you are not assigning the right value to $user_id, you have to make sure to transfert the value of the user ID to the page of relationship query by using a POST or GET method, or by retreving it directly from the DB