I have the following form automatically generated in a WHILE using $i++.
<form method="POST" id="form" >
<input type="text" name="id1" value="1" />
<input type="text" name="q1" value="5" />
<input type="text" name="a1" value="p" />
<input type="text" name="id2" value="4" />
<input type="text" name="q2" value="3" />
<input type="text" name="a2" value="t" />
<input type="text" name="id3" value="8" />
<input type="text" name="q3" value="7" />
<input type="text" name="a3" value="z" />
.....................
</form>
I insert the values into mysql using:
$id1=$_POST['id1'];
$q1=$_POST['q1'];
$a1=$_POST['a1'];
$query = "UPDATE table SET answer = '$a1' WHERE question = '$q1' and id= '$id1'";
mysqli_query($con,$query) or die(mysqli_error());
$id2=$_POST['id2'];
$q2=$_POST['q2'];
$a2=$_POST['a2'];
$query = "UPDATE table SET answer = '$a2' WHERE question = '$q2' and id= '$id2'";
mysqli_query($con,$query) or die(mysqli_error());
$id3=$_POST['id3'];
$q3=$_POST['q3'];
$a3=$_POST['a3'];
$query = "UPDATE table SET answer = '$a3' WHERE question = '$q3' and id= '$id3'";
mysqli_query($con,$query) or die(mysqli_error());
..........................
It's working just fine but how could I insert the values with only one query and WHILE or foreach? I tried to do like this but something is wrong:
$stmt = $this->mysqli->prepare("UPDATE table SET answer=? WHERE question=? and id =?");
foreach ($_POST['id'] as $i => $n){
$stmt->bind_param("sss", $n, $_POST['question'][$i], $_POST['answer'][$i], $id);
$stmt->execute();
}