Php 字段列表中的Sql错误未知列
我正在从sql数据库生成问题。我试图用问题id保存每个答案 我得到这个错误:错误:“字段列表”中的未知列“答案文本”为什么Php 字段列表中的Sql错误未知列,php,sql,Php,Sql,我正在从sql数据库生成问题。我试图用问题id保存每个答案 我得到这个错误:错误:“字段列表”中的未知列“答案文本”为什么 //save text input to sql with question id and question answer $question_id = mysqli_real_escape_string($con, $_POST['question_id']); foreach ($_POST['answer_text'] as $question_id =>
//save text input to sql with question id and question answer
$question_id = mysqli_real_escape_string($con, $_POST['question_id']);
foreach ($_POST['answer_text'] as $question_id => $answer_id){
$sql="INSERT INTO answers (question_id, answer_text)
VALUES ({$question_id}, {$answer_id})";
echo $sql;
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
}
mysqli_close($con);
// generate question from database
if ($result) {
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$body = $row['question_body'];
$question_id = $row['question_id'];
echo '<div class="questions-4">';
echo '
<tr>
<td>'.$body.'</td>
<td><input type="text" name="answer_text['.$question_id.']" </td><br>
</tr>';
echo '</div>';
您易受攻击,并且没有引用查询中使用的值。您正在生成以下等效项:
INSERT ... VALUES(foo, bar)
而不是
INSERT ... VALUES('foo', 'bar');
至少,为了解决眼前的问题,你应该
VALUES ('{$question_id}', '{$answer_id}')";
^--------------^--^------------^--- these
这就解决了它:
$sql="INSERT INTO answers (question_id, answer_value)
VALUES ({$question_id}, '$answer_id')";
我试过了,但是:错误:您的SQL语法有错误;检查与MySQL服务器版本相对应的手册,以了解第2行11',c附近使用的正确语法