Jump to content

How to do a INSERT with two different loops?


Annuscha

Recommended Posts

Hi,

 

I have been sitting with this code for two days now, if someone could please help me.

 

At the moment I'm getting two arrays from the form, my code works but the question id is 0 when inserting to the database. :'(

 

My code:

 

$sql_data = array();
$sql_prefix = "INSERT INTO student_score(course_uid, student_uid, question_uid, answer) VALUES";
foreach($_POST['answer'] as $id => $answer){
// don't use $_REQUEST!
$course_id = (int) $_POST['course_id'][$i];
$student_id  = (int) $_POST['student_id'][$i];
$question_id   = (int) $_POST['question_id'][$i];
$answer      = mysql_real_escape_string($answer);
$sql_data[] = "($course_id, $student_id, $question_id, '$answer')";

echo "\nvariable: ".$question_id."\nPOST: ".$_POST['question_id'][$i]."\n";

foreach ($_POST['question_id'] as $question_id) {

}
}
$sql = $sql_prefix.implode(", \n", $sql_data);
//var_dump($sql);
if(!mysql_db_query($dbName, $sql, $connect)){
$_SESSION['msg'] = "Could not save information, Please try again";
header("Location:student_assignment.php");
//replaced die with else clause
}
else{
$_SESSION['msg'] = "Question successfully created";
header("Location:student_assignment.php");
}

 

If I use var_dump($sql); below $sql it gives me this:

 

string(122) "INSERT INTO student_score(course_uid, student_uid, question_uid, answer) VALUES(1, 4, 0, 'answer1'), (1, 4, 0, 'answer2')" 

 

And if I var_dump($question_id) in the question_id foreach it gives me this:

 

string(2) "22" string(2) "23" string(2) "22" string(2) "23" string(122)

 

So it means it gets the $question values but it is making it 0 in the INSERT, can someone please help me to get the values in my table. :-[

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.