0

I'm trying to use PDO to insert data into my database but I'm getting this error

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 '' at line 1 in C:\xampp\htdocs\pfe\users\execute.php:21 Stack trace: #0 C:\xampp\htdocs\pfe\users\execute.php(21): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\pfe\users\execute.php on line 21'

this is my code :

<?php session_start(); require_once("database.php"); $req = $dbh ->prepare('INSERT INTO idad (etat, description, image, localisation, statut, categorie, author_num, created_at) VALUES(:etat, :description, :image, :localisation, :statut, :categorie, :author_num, NOW() '); $req ->bindParam(':etat' , $_POST["etat"]); $req ->bindParam(":description" , $_POST["description"]); $req ->bindParam(":image" , $_POST["image"]); $req ->bindParam(":localisation" , $_POST["localisation"]); $req ->bindParam(":statut" , $config['STATUS'][0]); $req ->bindParam(":categorie" , $_POST["categorie"]); $req ->bindParam(":author_num" , $_SESSION["id"]); $req ->execute(); var_dump($_POST); var_dump($_SESSION); var_dump($config); ?> 
6
  • 1
    You are missing a close ) for the values in , NOW() '); Commented Jun 2, 2021 at 10:12
  • So NOW() )'); Commented Jun 2, 2021 at 10:16
  • thank u this problem is solved but there is another one Can I Get Some Help this is the problem atal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'author_num' cannot be null in C:\xampp\htdocs\pfe\users\execute.php:21 Stack trace: #0 C:\xampp\htdocs\pfe\users\execute.php(21): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\pfe\users\execute.php on line 21 Commented Jun 2, 2021 at 10:28
  • That suggests that $_SESSION["id"] was null, don't you think Commented Jun 2, 2021 at 11:53
  • n no it's not null, but it's import from another table i don't know why it doesn't work Commented Jun 2, 2021 at 12:41

1 Answer 1

-1

Instead of having multiple lines of bindParam, did you try :

$req -> execute(array( ':param1' => $my_param1, ':param2' => $my_param2, ':param3' => $my_param3, )); 

I'm not sure your solution work or not, it's just I always did it my way because got recommended to do it this way, for security purposes if I'm right (still learning).

Also my code may be clearer and if I'm right about it, your code doesn't verify your $_POST variables aren't undefined nor empty so maybe it will lead to problems one day.

Sign up to request clarification or add additional context in comments.

1 Comment

There's no difference between your way and bindParam, it's just different syntax, the result is identical

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.