I'm still learning the basics at the moment and am making a simple system to store grades and student details entered on a PHP page. Basically, i want some of the input to go to one table called "student" and other bits to go to a table called "grades". Both contain a field called "student_id", the one in the grades table is a foreign key of the one in the student table.
I am using this code to POST information correctly:
That inputs the data correctly and it goes to the correct table. However, when I enter a second piece of information, I get an error telling me it can't enter data into the grades table. This is because the primary key isn't auto updating and is still "0". Therefore, as it's set to unique, it can't have dupe fields.
When data is posted to the student table, it correctly applies a new, unique primary key number called "student_id". How do I get it to input the exact same number into the "student_id" field in the grades table (student_id in the grades table is a foreign key of student_id in student table)
I'm pretty sure i've explained this really badly, so just ask and i can provide more info
I am using this code to POST information correctly:
Code:
$sql="INSERT INTO student (firstname, lastname, emailaddress, telnumber, username, password) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[emailaddress]','$_POST[telnumber]','$_POST[username]','$_POST[password]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "Your record has been added!"; $sql="INSERT INTO grades (maths, english, science, gradescore, student_id) VALUES ('$_POST[maths]','$_POST[english]','$_POST[science]','$_POST[gradescore]','$_POST[student_id]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "Your record has been added!";
When data is posted to the student table, it correctly applies a new, unique primary key number called "student_id". How do I get it to input the exact same number into the "student_id" field in the grades table (student_id in the grades table is a foreign key of student_id in student table)
I'm pretty sure i've explained this really badly, so just ask and i can provide more info

Comment