0

我有一个返回多个值的表单,每个值我想存储在数据库中,

我为此目的创建了一个测试表

<form method="post" action="{{ path('submit_exercise', {'page': 1}) }}">
    <input type="hidden" name="answers[]" value="Answer 1" "/>
    <input type="hidden" name="answers[]" value="Answer 2" "/>
    <input type="hidden" name="answers[]" value="Answer 3" "/>
    <input type="hidden" name="answers[]" value="Answer 4" "/>
    <input type="hidden" name="answers[]" value="Answer 5" "/>
    <input type="hidden" name="answers[]" value="Answer 6" "/>
    <input type="hidden" name="answers[]" value="Answer 7" "/>
    <input type="submit" name="submit" />
</form>
</body>
</html>

我的提交答案 Action 目前是这样写的。

public function submitAnswersAction($page)
{
    //get submitted data
    $data = $this->get('request')->request->all();
    $answers = $data['answers'];

    //get student ID
    $user = $this->get('security.context')->getToken()->getUser();
    $studentID = $user->getId();

    //Get Current time
    $currentTime = new \DateTime(date("Y-m-d H:m:s"));


    //var_dump($answers);
    //var_dump($studentID);
    //var_dump($currentTime);

    for($index = 0; $index < count($answers); $index++)
    {
        /*echo "Question ". ($index + 1) ."<br />";
        echo "Student ID: ". $studentID."<br />";
        echo "Page Number: $page <br />";
        echo "Answer: $answers[$index]"."<br />";
        echo "<br />";*/

        $studentAnswer = new StudentAnswer();
        $studentAnswer->setStudentID($studentID);
        $studentAnswer->setPageID($page);
        $studentAnswer->setQuestionID($index+1);
        $studentAnswer->setAnswer($answers[$index]);
        $studentAnswer->setDateCreated($currentTime);
        $studentAnswer->setReadStatus(0);

        $database = $this->getDoctrine()->getManager();
        $database->persist($studentAnswer);
        $database->flush();

    }

    return new Response('Answers saved for Student: '.$user->getFullName().' for page: '.$page);

当我执行 var_dump 时,一切似乎都正确关联,这意味着 answers 数组填充了正确的数据,其他所有变量也是如此,我的问题实际上是将它保存到数据库中。运行时它返回此错误,在我看来它不知道要放入行中的变量。

An exception occurred while executing 'INSERT INTO Student_Answer (student_id, page_id, question_id, answer, read, date_created) VALUES (?, ?, ?, ?, ?, ?)' with params {"1":2,"2":"1","3":1,"4":"Answer 1","5":0,"6":"2012-12-11 12:12:20"}:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'read, date_created) VALUES (2, 1, 1, 'Answer 1', 0, '2012-12-11 15:12:51')' at line 1

任何帮助都将不胜感激,因为这是一个个人项目,可帮助我尝试和了解更多 Web 开发。

4

2 回答 2

4

不确定是什么问题。一些东西:

这在循环之前:

$database = $this->getDoctrine()->getManager();

它应该是 $em 或 $entityManager (因为这就是它的名称......)

应该在循环之后调用:

$database->flush();

您不需要使用 Doctrine2 处理保存在数据库中的 ID。所以不要这样做:

$studentAnswer->setStudentID($studentID);

改为这样做:

$studentAnswer->setStudent($student);
于 2012-12-11T05:27:31.147 回答
1

哇,好吧,所以这个问题根本与 Symfony 或 Doctrine 无关,

问题基本上是该read字段,我相信它是 SQL 中的一个关键字,因此它将它读作关键字而不是列字段名称。我不得不将其重命名为is_read

于 2012-12-11T10:40:19.587 回答