Sql错误字段列表中的未知列

时间:2014-08-25 15:33:12

标签: php sql

我正在从sql数据库生成问题。我正试图用问题ID保存每个答案。

我收到此错误:“错误:'字段列表'中的未知列'answer_text'”为什么会这样?

 //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>';

2 个答案:

答案 0 :(得分:0)

您很容易受SQL injection attacks攻击,并且未引用您在查询中使用的值。您正在生成相当于:

INSERT ... VALUES(foo, bar)

而不是

INSERT ... VALUES('foo', 'bar');

至少,为了解决你的问题,你应该

VALUES ('{$question_id}', '{$answer_id}')";
        ^--------------^--^------------^--- these

答案 1 :(得分:0)

这解决了它:

        $sql="INSERT INTO answers (question_id, answer_value)
                VALUES ({$question_id}, '$answer_id')";