Bonjour à tous,

Incompréhensiblement je reçois une erreur si j'essaye d'utiliser la méthode PDO, pourtant le nombre de variables correspond bien au nombre de bindvalues, serait-ce une erreur de syntaxe ?

Erreur :
"Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in D:\Dev\Comments\comments.inc.php:23 Stack trace: #0 D:\Dev\Comments\comments.inc.php(23): PDOStatement->execute() #1 D:\Dev\Comments\index.php(45): setComments(Object(PDO)) #2 {main} thrown in D:\Dev\Comments\comments.inc.php on line 23"
NB: en query simple tout fonctionne correctement ...

Merci d'avance pour vos suggestions

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function setComments($conn)
{
    if (isset($_POST['commentSubmit'])) {
        $userId = $_POST['userId'];
        $createdOn = $_POST['createdOn'];
        $comment = $_POST['comment'];
 
        /*$sql = "INSERT INTO comments (userId, createdOn, comment)
        VALUES('$userId','$createdOn','$comment')";
        $result = $conn->query($sql);*/
 
 
 
        $sql = "INSERT INTO comments (userId, createdOn, comment)
        VALUES(':userId',':createdOn',':comment')";
 
        $stmt = $conn->prepare($sql);
        $stmt->bindValue(':userId', $userId, PDO::PARAM_STR);
        $stmt->bindValue(':createdOn', $createdOn, PDO::PARAM_STR);
        $stmt->bindValue(':comment', $comment, PDO::PARAM_STR);
        $stmt->execute();
    }
}