My array is something like this
Array
(
[0] => Array
(
[questionNumber] => 1
[question] => Which paper are your preparing for?
[option] => IELTS General
)
[1] => Array
(
[questionNumber] => 2
[question] => How much time do you have at hand to complete your preparation?
[option] => 1 to 2 months
)
[2] => Array
(
[questionNumber] => 3
[question] => Have you taken the IELTS exam earlier?
[option] => Yes
)
[3] => Array
(
[questionNumber] => 4
[question] => How many attempts did you take?
[option] => 2
)
[4] => Array
(
[questionNumber] => 5
[question] => What were your overall band scores
[option] => 5 to 6 bands
)
[5] => Array
(
[questionNumber] => 6
[question] => Which module do you need coaching for?
[option] => Specific Modules only
)
)
My SQL table looks something like this
id | email | q1 | a1 | q2 | a2 | q3 | a3 | q4 | a4 | q5 | a5 | q6 | a6
I want to add question number 1 question element to q1 column and its option to a1 column. similarly add question number 2 question element to q2 and its option to a2 column and repeat this process till last question.
My php code is given below
<?php
$servername = 'localhost';
$dbname = 'chartjs';
$username = 'root';
$password = '';
//----------------------------------------------------------Connection Part---------------------------------------------------
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(!$conn){
die("Connection Failed". mysqli_connect_error());
}
if(isset($_POST["response"])) {
$responsearray=json_decode($_POST['response'], true);
print_r($responsearray);
foreach($responsearray as $response){
$questionNumber =($response['questionNumber']);
$question =($response['question']);
$option =($response['option']);
$query="INSERT INTO `ielts_quiz_db`(`q1`, `a1`) VALUES (' $question','$option') ";
$result=mysqli_query($conn,$query);
}
}
?>