Php mysqli | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

4th Jun 2018, 4:48 PM
Twumasi Yeboah Jr
Twumasi Yeboah Jr - avatar
31 Answers
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
4th Jun 2018, 7:06 PM
Mohamed Elhachemi
Mohamed Elhachemi - avatar
0
the insert works when the foreign key isnt set on the child table. i dont know why
4th Jun 2018, 7:10 PM
Twumasi Yeboah Jr
Twumasi Yeboah Jr - avatar
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
4th Jun 2018, 8:16 PM
Mohamed Elhachemi
Mohamed Elhachemi - avatar
0
i tried that but didnt work. could there be another reason?
4th Jun 2018, 8:25 PM
Twumasi Yeboah Jr
Twumasi Yeboah Jr - avatar
0
sure that could be another reason
4th Jun 2018, 8:30 PM
Mohamed Elhachemi
Mohamed Elhachemi - avatar
0
could it be that because they’re on different pages? the values don’t insert on the page with the foreign key table
4th Jun 2018, 8:33 PM
Twumasi Yeboah Jr
Twumasi Yeboah Jr - avatar
0
send me your two tables structure and your insert request, and i will have a look later
4th Jun 2018, 8:35 PM
Mohamed Elhachemi
Mohamed Elhachemi - avatar
0
ok
4th Jun 2018, 8:40 PM
Twumasi Yeboah Jr
Twumasi Yeboah Jr - avatar
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)
4th Jun 2018, 8:57 PM
Twumasi Yeboah Jr
Twumasi Yeboah Jr - avatar
0
you think i should include the mysqli_insert_id() ?
4th Jun 2018, 8:58 PM
Twumasi Yeboah Jr
Twumasi Yeboah Jr - avatar
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
4th Jun 2018, 10:28 PM
Mohamed Elhachemi
Mohamed Elhachemi - avatar
0
how do you mean?
4th Jun 2018, 10:31 PM
Twumasi Yeboah Jr
Twumasi Yeboah Jr - avatar
0
you can use mysqli_insert_id() if the INSERT INTO users is the last query before the INSERT INTO relationship query
4th Jun 2018, 10:35 PM
Mohamed Elhachemi
Mohamed Elhachemi - avatar
0
echo $sql;
4th Jun 2018, 10:39 PM
Mohamed Elhachemi
Mohamed Elhachemi - avatar
0
the user-id which is the foreign key only prints out as zero as the value
4th Jun 2018, 10:53 PM
Twumasi Yeboah Jr
Twumasi Yeboah Jr - avatar
0
sorry for the late answer, you have to assign the right value to $user-id before using it in the query
5th Jun 2018, 5:01 PM
Mohamed Elhachemi
Mohamed Elhachemi - avatar
0
i did assign it by using the mysqli_inser_id(), but still didnt work
5th Jun 2018, 5:05 PM
Twumasi Yeboah Jr
Twumasi Yeboah Jr - avatar
0
did you verify the query with echo $sql after using the mysqli_insert_id
5th Jun 2018, 5:42 PM
Mohamed Elhachemi
Mohamed Elhachemi - avatar
0
yeah, and the value for the userid was ‘0’
5th Jun 2018, 5:52 PM
Twumasi Yeboah Jr
Twumasi Yeboah Jr - avatar
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
5th Jun 2018, 6:07 PM
Mohamed Elhachemi
Mohamed Elhachemi - avatar