Home > Software design >  Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in
Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in

Time:06-26

I'm trying to upload information to my DB but it keeps giving me a syntax error.

$query = "INSERT INTO `klant` (`naam`,`adres`,`postcode`,`email`,`nieuwsbrief`) VALUES ($naam,$adres,$postcode,$plaats,$email,$nieuwsbrief)";

The query I use should work as it's the same as in PHPMyAdmin.

The error I receive:

PHP Fatal error: Uncaught PDOException: 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 '4,1234AB,Rotterdam,[email protected],1)'

CodePudding user response:

Two errors:

  • There are five columns specified in your insert query, but you're trying to put in six values. They should match. (plaats is missing)
  • String values should have quotes "" around them in insert statements.

Also, rickdenhaan touched on a good point. Using variables like this is dangerous as it allows for SQL injection, especially if the variables are populated by the public.

  •  Tags:  
  • php
  • Related