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:

3
  • 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. Commented Mar 17, 2014 at 13:02
  • How does look multiple record which you are adding, coz there's no loop in form, or in inserting results!! Commented Mar 17, 2014 at 13:03
  • Do you receive an error? Commented Mar 17, 2014 at 13:13

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

check var_dump($_POST['mark']) at the beginning of DB script.
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')");

1 Comment

Looks like the body of the question is deleted.
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.

1 Comment

There is mysql_escape_string although I agree the real one does a better job, but that doesn't concern the OP's problem.
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.