-1

I want to prepare an SQL insert statement inside PHP for loop, my statement is something like this

$insert_stmt = "";
for($x=1; $x<=$all_property[duration]; $x++) {
    $insert_stmt .= "INSERT INTO `schedule` VALUES ($all_property[serial], $_POST[Day$x])";

echo $insert_stmt;
mysqli_free_result($insert_stmt);   // Free result set for next query
}

wherein I expect the result to be like

INSERT INTO `schedule` VALUES ($all_property[serial], $_POST[Day1] //for first loop
INSERT INTO `schedule` VALUES ($all_property[serial], $_POST[Day2] //for second loop

And so on, but I am getting error

syntax error, unexpected '$x' (T_VARIABLE), expecting ']' in schedule.php on line 163

so, if any one can help me rectify what is wrong in my query.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
Nilesh
  • 73
  • 6

1 Answers1

1

Updated:

As the fields are not constant and also they are string, you need to quote them, so just use this line:

$insert_stmt = "INSERT INTO schedule VALUES (‘".$all_property[serial]."’, ‘".$_POST["Day".$x]."’)";
Amir MB
  • 3,233
  • 2
  • 10
  • 16