Question

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. :)

Was it helpful?

Solution

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.

OTHER TIPS

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]');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top