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

Asked

Viewed 13,368 times

2

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

  1. Warning: Pdostatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid Parameter number: number of bound variables does not match number of tokens in /localhost/admin/crud/edit.php on line 33
  2. Warning: Cannot Modify header information - headers already sent by (output Started at localhost/admin/crud/edit.php:33) in localhost/admin/crud/edit.php on line 36

What was done: I just added a select to save the data to 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;
}
  • Line 33 is the fourth of the part you posted here?

  • @bfavaretto this code is working together with html

2 answers

5


This error happens when there is a parameter more or less 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 the bindParam():

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

To resolve this simply remove the sixth bindParam. the Second error is the consequence of the first

  • the error is really this, now in select view if I edit it I see different values I entered.

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/edit.php on line 33"

Yes, because you have a query with 5 parameters, and are giving bind 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']);
  • I fixed that too, thanks @

Browser other questions tagged

You are not signed in. Login or sign up in order to post.