0

Hi all I have a form where I'm entering students marks for exams, what I am trying to do is enter multiple marks in at once...but it just is not working, could someone help please? It is adding only ONE result, doesn't enter all that I write in.

FORM Code:

  • Looks like you compiled this code from different sources and forgot the essential. You are using HTML input arrays which hold multiple values thus you need to loop through the data and save each. – AyB Mar 17 '14 at 13:02
  • How does look multiple record which you are adding, coz there's no loop in form, or in inserting results!! – Michael Mar 17 '14 at 13:03
  • Do you receive an error? – Tim Ferrell Mar 17 '14 at 13:13

4 Answers4

2

The script does not know what is $i in this case.

Use for loop:

$mark=$_POST['mark'];
$time=$_POST['time'];
$meID=$_POST['meID'];
$sID=$_POST['sID'];

for($i = 0; $i < count($_POST['mark']); $i++) {
    $_mark = mysql_escape_string($mark[$i]);
    $_time = mysql_escape_string($time[$i]);
    $_meID = mysql_escape_string($meID[$i]);
    $_sID = mysql_escape_string($sID[$i]);

    $sql = "INSERT INTO Marks (mark, time, meID, sID) VALUES ($_mark, $_time, $_meID, $_sID)";
    $result = mysql_query($sql);
}

Additionally you will add each mark twice, because of two mysql_query() invocations. I've already deleted one from $sql variable in my answer. Also you have wrong variable names in the insert query.

michal.hubczyk
  • 698
  • 3
  • 9
1

there is no mysql_escape_string . try replace this;

   $_mark = mysql_escape_string($mark[$i]);

to

   $_mark = mysql_real_escape_string($mark[$i]);

and put $_mark in the query instead of $mark

and replace all others also.

echo_Me
  • 37,078
  • 5
  • 58
  • 78
  • 1
    There is [mysql_escape_string](http://php.net/mysql_escape_string) although I agree the _real_ one does a better job, but that doesn't concern the OP's problem. – AyB Mar 17 '14 at 13:07
1

I think the problem is with the variable names:

Following line:

$sql = "SELECT * FROM ModuleExamStudent WHERE mesID = '$mesID'";

But there is no $mesID. It should be $mes:

$sql = "SELECT * FROM ModuleExamStudent WHERE mesID = '$mes'"; 

Again:

$sql = mysql_query("INSERT INTO Marks (mark, time, meID, sID) VALUES ('$mark', '$time', '$meID', '$sID')");

Should be:

$sql = mysql_query("INSERT INTO Marks (mark, time, meID, sID) VALUES ('$_mark', '$_time', '$_meID', '$_sID')");
Ambrish
  • 3,627
  • 2
  • 27
  • 42
0

Yeah, a lot of mistakes in you're code. Fuse michal.hubczyk and Ambrish answer to obtain wath you want. If i can suggest another way to do it, i'll prefer to make only one query instead an elevated number of query succession inside a loop.

Try this method Inserting multiple rows in a single SQL query? and build your query in a way like this:

$sql = "INSERT INTO XX VALUES";
for($i=0;$i < count($_POST['mark']); $i++){
 $_mark = mysql_escape_string($mark[$i]);
 $_time = mysql_escape_string($time[$i]);
 $_meID = mysql_escape_string($meID[$i]);
 $_sID = mysql_escape_string($sID[$i]);
 $sql .= "(".$_mark.",".$_time",".$_meID.",".$sID.")";
 if($i<(count($_POST['mark']-1))){
  $sql .= ",";
}
$result = mysql_query($sql);

Another suggestion is to use mysqli libray instead mysql ( http://php.net/mysqli)

Community
  • 1
  • 1
Andrea_86
  • 489
  • 5
  • 19