简体   繁体   中英

PDO prepared statement Error 1064

I getting this error trying to insert data into my table.

Array ( [0] => 42000 [1] => 1064 [2] => 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 'username , password , firstname , lastname , email , emailcode``) VALUES ('' at line 1 )

My code looks like this .. $reg_data is an array that contains the data from $_POST.

        $fields = '`' . implode('`, `',array_keys($reg_data)) . '`';
        $data = '\'' . implode('\', \'', $reg_data) . '\'';


        $prep = $this->db->prepare('INSERT INTO `users` (`'.$fields.'`) VALUES (?)');

        $prep->bindParam(1, $data);

        $prep->execute();

        print_r($prep->errorInfo());

As you can see the error it clearly shows you the use of double back-tics emailcode``

Once you have added backtiks in the implode you are adding again in insert query ( '.$fields.' )

change

 $fields = '`' . implode('`, `',array_keys($reg_data)) . '`';

to

 $fields =  implode('`, `',array_keys($reg_data)) ;

Or just use it without bacticks as you have already added using implode

$prep = $this->db->prepare('INSERT INTO `users` ('.$fields.') VALUES (?)');

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