i have a table and it display all the record in the table using $row. It generates multiple lines because it displayed all the value from the table. Then inside the table i created a form that comprised of only one single row. I want to record one value of that single $row using the studentnumber and subjectcode which is both from the $row in my query. Sorry for my confusing question and THANKS in advance.
code:
echo "<form name=\"grades\" action=\"insert.php\" method=\"post\" onsubmit=\"javascript:return confirm('Confirm Grade Submission?');\">";
echo "<td class=\"studentlist\">" . $row['studentnumber'] . "</td>";
echo "<td class=\"studentlist\">" . $row['subject'] . "</td>";
echo "<td class=\"studentlist\"><input type=\"text\" id= \"q1\" name=\"q1\" class=\"tbox\"></td>";
echo "<td class=\"studentlist\"><input type=\"text\" id= \"q2\" name=\"q2\" class=\"tbox\"></td>";
echo "<td class=\"studentlist\"><input type=\"text\" id= \"q3\" name=\"q3\" class=\"tbox\"></td>";
echo "<td class=\"studentlist\"><input type=\"submit\" value=\"Submit\"></td>";
echo "</form>";
insert.php
$studentnumber= $_POST['studentnumber'];
$subjectcode= $_POST['subjectcode'];
$query = ("SELECT * FROM grades WHERE studentnumber = '$studentnumber' && subjectcode = '$subjectcode'");
$result=mysql_query($query);
if($result)
{
if(mysql_num_rows($result) == 1)
{
$sql="INSERT INTO grades ( q1, q2, q3 )
VALUES('$_POST[q1]','$_POST[q2]','$_POST[q3]'')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "<script type='text/javascript'>alert('Grade Successfully Submitted');location.href = '../administrator/admin_home.php';</script>";
}
}
First things first, you will need to pass the studentnumber and subjectcode from the form to the second page which processes the MySQL. For this you can use some hidden form fields, i.e. use in place of the lines where you echo studetnumber and subject.
echo "<td class=\"studentlist\"><input type=\"hidden\" name=\"studentnumber\" value=\" . $row['studentnumber'] . \">" . $row['studentnumber'] . "</td>";
echo "<td class=\"studentlist\"><input type=\"hidden\" name=\"subject\" value=\" . $row['subject'] . \">" . $row['subject'] . "</td>";
It's hard to be specific without seeing the full table design of grades, however the following would insert the data you are trying to insert however it would error if studentnumber and subjectcode formed a UNIQUE key, and the row already existed
$sql = "INSERT INTO grades (
studentnumber,
subjectcode,
q1,
q2,
q3
) VALUES (
'".$_POST['studentnumber']."',
'".$_POST['subject']."',
'".$_POST['q1']."',
'".$_POST['q2']."',
'".$_POST['q3']."'
)";
In order, if it hits a duplicate studentnumber/studentcode combination, to make it update then you just add "ON DUPLICATE KEY" syntax to the end, i.e.
$sql = "INSERT INTO grades (
studentnumber,
subjectcode,
q1,
q2,
q3
) VALUES (
'".$_POST['studentnumber']."',
'".$_POST['subject']."',
'".$_POST['q1']."',
'".$_POST['q2']."',
'".$_POST['q3']."'
)
ON DUPLICATE KEY UPDATE
q1 = VALUES(q1),
q2 = VALUES(q2),
q3 = VALUES(q3)";
This will then update q1,q2,q3 with whatever values were specified for the row(s) that had duplicate student number/subject combinations.
Finally if you just want to ignore duplicates, then the following will suffice (note addition of IGNORE).
$sql = "INSERT IGNORE INTO grades (
studentnumber,
subjectcode,
q1,
q2,
q3
) VALUES (
'".$_POST['studentnumber']."',
'".$_POST['subject']."',
'".$_POST['q1']."',
'".$_POST['q2']."',
'".$_POST['q3']."'
)";