Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO UPDATE: Syntax error or access violation [closed]

Tags:

sql

php

mysql

pdo

I'm new in PDO, writing an update query:

$sql = "UPDATE `users` 
           SET(`uname` = :uname,
               `role` = :role,
               `fname` = :fname,
               `email` = :email,
               `mobile1` = :mobile1,
               `mobile2` = :mobile2,
               `education` = :education,
               `division` = :division,
               `district` = :district,
               `sub_district` = :sub_district,
               `address` = :address,
               `looking_for` = :looking) 
         WHERE `id` = :id";

            //$sql = "UPDATE `users` SET(`uname`=?,`role`=?,`fname`=?,`email`=?,`mobile1`=?,`mobile2`=?,`education`=?,`division`=?,`district`=?,`sub_district`=?,`address`=?,`looking_for`=?) WHERE `id`=?";
            $st = $conn->prepare($sql);
            //$res['sql'] = $st->queryString;
            $params = array(
                ':uname' => $uname, 
                ':role' => $role, 
                ':fname' => $fname, 
                ':email' => $email, 
                ':mobile1' => $mobile1, 
                ':mobile2' => $mobile2, 
                ':education' => $edu, 
                ':division' => $division, 
                ':district' => $district, 
                ':sub_district' => $sub_district, 
                ':address' => $address, 
                ':looking' => $looking, 
                ':id' => $id
            );

            //$res['params'] = $params;
            $r = $st->execute($params);     

And getting:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(uname = '[email protected]',role = '2',fname = 'A full Name',email = 'rahm' at line 1

Can anybody tell me what's wrong in my code?

Here is my table structure:

enter image description here

like image 361
A. K. M. Tariqul Islam Avatar asked Nov 08 '15 09:11

A. K. M. Tariqul Islam


1 Answers

Remove brackets around SET part:

$sql  =  "UPDATE `users` 
          SET  `uname` = :uname,
               `role` = :role,
               `fname` = :fname,
               `email` = :email,
               `mobile1` = :mobile1,
               `mobile2` = :mobile2,
               `education` = :education,
               `division` = :division,
               `district` = :district,
               `sub_district` = :sub_district,
               `address` = :address,
               `looking_for` = :looking 
         WHERE `id` = :id";

Check UPDATE syntax:

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
    SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]
like image 190
Lukasz Szozda Avatar answered Nov 02 '22 06:11

Lukasz Szozda