【发布时间】:2021-11-12 08:06:30
【问题描述】:
我的数组是这样的
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
)
)
我的 SQL 表看起来像这样
id | email | q1 | a1 | q2 | a2 | q3 | a3 | q4 | a4 | q5 | a5 | q6 | a6
我想将 question number 1 问题元素添加到 q1 列,并将其选项添加到 a1 列。 同样将 question number 2 问题元素添加到 q2 并将其选项添加到 a2 列并重复此过程直到最后一个问题。
我的php代码如下
<?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);
}
}
?>
【问题讨论】:
-
警告:您对SQL Injections 持开放态度,应该使用参数化的prepared statements,而不是手动构建查询。它们由PDO 或MySQLi 提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your data。 Escaping is not enough!