Number of bound variables does not match number of tokens

Why I am getting this error in code

 $sqlInsert = "INSERT INTO users(username, email, password, join_date) ";
        $sqlInsert.= "VALUES(:username, :email, :password, :now())";

    $statement = $db->prepare($sqlInsert);
    $statement->execute(array(':username' => $username,
                            ':email' => $email, 
                            ':password' => $password
                            ));

OK SOLVED - removed colon from now() function… :wink:

Because you are writing incorrect syntax.

As a rule of thumb, if you cannot manage to write two equal sets of named placeholders, better go for positional ones - there is simply less space for the error:

$sql = "INSERT INTO users(username, email, password, join_date) VALUES(?,?,?, now())";
$statement = $db->prepare($sql);
$statement->execute(array($username,$email, $password));
1 Like

Thanks, I will write this in note.

I have one more problem

Integrity constraint violation: 1062 Duplicate entry ‘hash’ for key ‘username’ in …

I am working on the registration form, the hash is the username if I try to again signup with the same username it shows me the error written above in bold.

I have set the username column to UNIQUE in db, I had also tried to make it INDEX but this doesn’t work.

try{
        $sqlInsert = "INSERT INTO users(username, email, password, join_date) ";
        $sqlInsert.= "VALUES(:username, :email, :password, now())";

    // santizing the data
    $statement = $db->prepare($sqlInsert);

    // add the data into database
    $statement->execute(array(':username' => $username,
                            ':email' => $email, 
                            ':password' => $hashed_password
                            ));
 
 // check if one new row was created
 if ($statement->rowCount() == 1){
     $result = "<p style='padding:20px; color: green;'>Registration Successful</p>";
 }
 
} catch(PDOExeception $e){
     $result = "<p style='padding:20px; color: red;'>An error occured: " . $e->getMessage()."</p>";
  }

} else {
    // if the number of errors equal to 1
    //if(!empty($form_errors)){
    if(count($form_errors) == 1){
        $result = "<p style='color:red;'>There was 1 error in the form<br>";
     
    } else {
        $result = "<p style='color:red;'>There were " . count($form_errors). " errors in the form <br>";

    }

Kindly help

And what else you expect it to do?

If similar username found, show the custom error “Username already exists” instead of this error.

Does this have something to do with enabling exception mode in PDO? http://php.net/manual/en/pdo.error-handling.php

In my tutorial I’ve got an example for this very case:

try {
    $pdo->prepare("INSERT INTO users VALUES (NULL,?,?,?,?)")->execute($data);
} catch (PDOException $e) {
    if ($e->getCode() == 1062) {
        // Take some action if there is a key constraint violation, i.e. duplicate name
    } else {
        throw $e;
    }
}

However, I suppose you may want to go a simpler road, by simply adding a select query during user input validation.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.