Inserting data in one table which has two foreign keys (php)

I'm new to this site, and I need some help with my record system. I need to insert the grades of the students into the grades table, which has the columns: gradeid (PK, auto_increment), studentid (FK), courseid (FK), midterm, endterm, final, remark.

The studentid (FK) gets the number from the studentid (PK) of addrec table.

The courseid (FK) gets the number from the courseid (PK) of course table.

Here are the codes that I have experimented (for the umpteenth time) but still got no results:

<?php
$host="localhost";  
$username="root"; 
$password=""; 
$db_name="studentrec";


$studentid = $_GET['id'];
$sql1="SELECT studentid FROM addrec WHERE studentid='$studentid'";

if(mysql_query($sql1))
{
$courseid = $_GET['courseid'];
$sql2 = "SELECT courseid FROM course WHERE courseid='$courseid'";

if (mysql_query ($sql2)) 
{
$sql3="INSERT INTO grades(`studentid`, `courseid`, `midterm`, `endterm`, `final`,    remark`) VALUES   ('$studentid','$courseid','$_POST[midterm]','$_POST[endterm]','$_POST[final]','$_POST[remark]' )";
}
if (mysql_query($sql3))
{
    // Success
}
else 
{
    die('Error on query 2: ' . mysql_error($con));
}
}
?>

There are no errors whenever I hit the Submit button, but the data I put in the text boxes won't insert into the grades table. Help, please? Or suggestions? I'm still studying stuff about PHP. Thank you. :)

Answers


youre not connecting to the mySQL : http://www.php.net/manual/en/function.mysql-connect.php

also you might notice the big red box on php.net, you should move to PDO or MySQLi.


Query contains error. Try this

INSERT INTO grades(studentid,courseid,midterm,endterm,final,remark) VALUES  ('$studentid','$courseid','$_POST[midterm]','$_POST[endterm]','$_POST[final]','$_POST[remark]' );

do your db connection first than select db and than

try to replace

$sql3="INSERT INTO grades(`studentid`, `courseid`, `midterm`, `endterm`, `final`,    remark`) VALUES   ('$studentid','$courseid','$_POST[midterm]','$_POST[endterm]','$_POST[final]','$_POST[remark]' )";

with

$sql3="INSERT INTO grades(`studentid`, `courseid`, `midterm`, `endterm`, `final`,    remark`) VALUES('$studentid','$courseid','".$_POST['midterm']."','".$_POST['endterm']."','".$_POST['final']."','".$_POST['remark']."' )";

You actual query is like

INSERT INTO grades(`studentid`, `courseid`, `midterm`, `endterm`, `final`,remark`) 
VALUES
('$studentid','$courseid','$_POST[midterm]','$_POST[endterm]','$_POST[final]','$_POST[remark]' );

You need to put opening ` (tild) before column remark, so your query will looks like

INSERT INTO grades(`studentid`, `courseid`, `midterm`, `endterm`, `final`,`remark`) 
VALUES
('$studentid','$courseid','$_POST[midterm]','$_POST[endterm]','$_POST[final]','$_POST[remark]');

Need Your Help

How to generate RSpec test cases for each value of ActiveModel's Inclusion validator

ruby-on-rails validation ruby-on-rails-4 rspec activemodel

I've got that simple ActiveModel class holding a string field on which I defined an inclusion validator:

Upgrading from Java 1.4 to JAVA 6 SE

java upgrade

I am currently upgrading from JAVA 1.4 to JAVA 6 SE. Some of the methods and classes have either been deprecated or there are a lot of warnings due to inefficient practices. I want to know if it is