Number of "bound variables" does not match the number of "tokens"

2

I made some changes to the script and it is generating two errors:

  • Warning: PDOStatement :: execute () [pdostatement.execute]: SQLSTATE [HY093]: Invalid parameter number: number of bound variables does not match number of tokens in /localhost/admin/crud/editar.php on line 33
  • Warning: Can not modify header information -

    What was done: I just added a select to save the data in the database. The script:

        if(!empty($_GET['codusuario']) && $_SERVER['REQUEST_METHOD'] == 'GET'){
        $stm = $pdo->prepare('SELECT * FROM usuario WHERE codusuario = ?');
        $stm->bindParam(1, $_GET['codusuario'], PDO::PARAM_INT);
        $stm->execute() or die(implode('', $pdo->errorInfo()));
    
        $_POST = $stm->fetch(PDO::FETCH_ASSOC);
    }
    
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        if(empty($_POST['codusuario'])){
            $sql = 'INSERT INTO usuario (email,senha,chave,select2,pergunta) VALUES (?, ?, ?, ?, ?)'; //email, senha, chave, pergunta
            $stm = $pdo->prepare($sql);
            $stm->bindParam(1, $_POST['email']);
            $stm->bindParam(2, $_POST['senha']);
            $stm->bindParam(3, $_POST['chave']);
            $stm->bindParam(4, $_POST['select2']); //@new select box
            $stm->bindParam(5, $_POST['pergunta']);
    
            $stm->execute();
        } else {
            $sql = 'UPDATE usuario SET email = ?, senha = ?, chave = ?, pergunta = ? WHERE codusuario = ?';
            $stm = $pdo->prepare($sql);
            $stm->bindParam(1, $_POST['email']);
            $stm->bindParam(2, $_POST['senha']);
            $stm->bindParam(3, $_POST['chave']);
            $stm->bindParam(4, $_POST['pergunta']);
            $stm->bindParam(5, $_POST['codusuario'], PDO::PARAM_INT); //old number is 5
            $stm->bindParam(6, $_POST['select2']);
            $stm->execute();
        }
    
        header('Location: index.php');
        exit;
    }
    
        
  • asked by anonymous 29.05.2014 / 04:58

    2 answers

    5

    This error happens when there is a more or less parameter in the query, in the update:

       UPDATE usuario SET email = ?, senha = ?, chave = ?, pergunta = ? WHERE codusuario = ? 
                                  1          2          3             4                    5 
    

    There are 5 questions and 6 variables in bindParam() :

    $stm->bindParam(6, $_POST['select2']);
    

    To solve this, simply remove the sixth bindParam. The second error is the consequence of the first

        
    29.05.2014 / 05:05
    4

    "Warning: PDOStatement :: execute () [pdostatement.execute]: SQLSTATE [HY093]: Invalid parameter number: number of bound variables does not match number of tokens in /localhost/admin/crud/editar.php on line 33 "

    Yes, because you have a query with 5 parameters, and you are binding in 6:

    $sql = 'UPDATE usuario SET email = ?, senha = ?, chave = ?, pergunta = ? WHERE codusuario = ?';
        $stm = $pdo->prepare($sql);
        $stm->bindParam(1, $_POST['email']);
        $stm->bindParam(2, $_POST['senha']);
        $stm->bindParam(3, $_POST['chave']);
        $stm->bindParam(4, $_POST['pergunta']);
        $stm->bindParam(5, $_POST['codusuario'], PDO::PARAM_INT); //old number is 5
        $stm->bindParam(6, $_POST['select2']);
    
        
    29.05.2014 / 05:05