HTML Input
<input type="checkbox" value="1" name="que[10][1]">
<input type="checkbox" value="2" name="que[10][2]">
<input type="checkbox" value="3" name="que[10][3]">
...
<input type="checkbox" value="40" name="que[10][40]">
PHP submit
$submit = $_POST["que"];
I have the following array I want to store in my database :
Array
(
[10] => Array
(
[0] => 26
[1] => 27
[2] => 28
[3] => 29
[4] => 30
[5] => 31
)
)
How can I convert to insert statement output following as below :
INSERT INTO `tb_answers` (`question_id`, `answer`) VALUES(10,26),(10,27),(10,28),(10,29),(10,30);
Here is my code :
$submit = $_POST['que'];
$all_values = [];
$sql_submit_form = "INSERT INTO `tb_answers` (`question_id`, `answer`) VALUES";
foreach ($submit as $question_id => $choise) {
$row_values = [];
foreach ($choise as $choise_data => $s_value) {
$row_values[] = "'" . $question_id . "'"; // question_id
$row_values[] = "'" . $s_value . "'"; // answer
}
$all_values[] = '(' . implode(',', $row_values) . ')';
}
$sql_submit_form .= implode(',', $all_values);
echo $sql_submit_form;
Output :
INSERT INTO `tb_answers` (`question_id`, `answer`) VALUES('10','27','10','28','10','29','10','30','10','31','10','32')