简体   繁体   中英

how to update mysql column using symfony

I created in view file html input form and I named it "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 %}

and in my default controller file

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));
}

but I get this error when I run my code

Type error: Argument 1 passed to Symfony\\Component\\Form\\FormRenderer::renderBlock() must be an instance of Symfony\\Component\\Form\\FormView, instance of Doctrine\\DBAL\\Driver\\PDOStatement given, called in /opt/lampp/htdocs/x/chessMult/1/sym/thesym/app/cache/dev/twig/96/96f6b6f8c21d5412246839a2d6e2eb66ac4141143dd67e2b91f285f2a31b60fc.php on line 44

You should use a Symfony Form . The returned data from the form can be flushed to update your database record. There is an example provided in the docs.

$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(),
));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM