[英]how to update mysql column using symfony
我在視圖文件html輸入表單中創建了一個表單,並將其命名為“ thenum”
{% extends 'base.html.twig' %}
{% block body %}
<h1>User edit</h1>
{{ form_start(edit_form) }}
{{ form_widget(edit_form) }}
<input type="text" name="thenum" value="0"><br>
<input type="submit" value="Edit" />
{{ form_end(edit_form) }}
{% endblock %}
並在我的默認控制器文件中
public function indexAction()
{
$user = $this->container->get('security.context')->getToken()->getUser();
$user->getId();
$conn = $this->get('database_connection');
$users = $conn->query("UPDATE user SET batman= 'thenum' WHERE username ='$user'");
return $this->render('siteblogBundle:Default:index.html.twig', array('edit_form' => $users));
}
但是我運行代碼時收到此錯誤
類型錯誤:傳遞給Symfony \\ Component \\ Form \\ FormRenderer :: renderBlock()的參數1必須是Symfony \\ Component \\ Form \\ FormView的實例,Doctrine \\ DBAL \\ Driver \\ PDOStatement的實例,在/ opt / lampp /中調用htdocs / x / chessMult / 1 / sym / thesym / app / cache / dev / twig / 96 / 96f6b6f8c21d5412246839a2d6e2eb66ac4141143dd67e2b91f285f2a31b60fc.php在第44行
您應該使用Symfony表單 。 表單中返回的數據可以刷新以更新數據庫記錄。 文檔中提供了一個示例。
$task = new Task();
$form = $this->createFormBuilder($task)
->add('task', TextType::class)
->add('dueDate', DateType::class)
->add('save', SubmitType::class, array('label' => 'Create Task'))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// $form->getData() holds the submitted values
// but, the original `$task` variable has also been updated
$task = $form->getData();
// ... perform some action, such as saving the task to the database
// for example, if Task is a Doctrine entity, save it!
// $em = $this->getDoctrine()->getManager();
// $em->persist($task);
// $em->flush();
return $this->redirectToRoute('task_success');
}
return $this->render('default/new.html.twig', array(
'form' => $form->createView(),
));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.