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 (?)');
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top