PHP/MYSQL - only last row is inserted.

Hi
I´m trying to insert a PHP array (school grades for one student) into Mysql. The array looks fine when I print it, and the sql consists of one insert per array row. However, only the last row of the array is inserted. What have I missed, and can anyone help?
regards,
ejret.

SQL TABLE:

CREATE TABLE students.grades (
id int(10) unsigned NOT NULL auto_increment,
subjid varchar(45) NOT NULL,
grade varchar(45) default NULL,
studid varchar(45) NOT NULL,
pid varchar(11) NOT NULL,
date timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=340 DEFAULT CHARSET=latin1

Main part of the form (a while loop):

echo "

$subject ". " $studid

";

Foreach loop and insert statement:

foreach ($_POST[‘grade’] as $int => $grade) {

$subjid = $_POST['subjid'][$int];

$pid=$_POST['pid'];

$studid= $_POST[‘studid’];

$sql = "INSERT INTO grades SET subjid='$subjid', studid='$studid',

grade=’$grade’, pid=’$pid’, date=NOW()";

Hey…
I think u messed up your PHP-MySQL code…
try this:
[php]
$date = now();
$sql = “INSERT INTO grades (subjid,studid,grade,pid,date) VALUES (’$subjid’,’$studid’,’$grade’,’$pid’,’$date’)”;
if(!mysql_query($sql)) {
die(“Could not insert into database”);
}
[/php]
instead of
[php]
$sql = “INSERT INTO grades SET subjid=’$subjid’, studid=’$studid’,
grade=’$grade’, pid=’$pid’, date=NOW()”;
[/php]

Hope this helps

Indeed it did help - so simple… I really should have seen it myself.
Thank you Ramanakumar!

ejret

Sponsor our Newsletter | Privacy Policy | Terms of Service