我有一个返回多个值的表单,每个值我想存储在数据库中,
我为此目的创建了一个测试表
<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 开发。