SQLSTATE[HY093]: Invalid Parameter number Insert problem

Asked

Viewed 14,345 times

5

I need to enter through a form the following data:

  • TITLE
  • DESCRIPTION
  • PRICE

HTML:

    <?php 
session_start();
session_destroy(); 
?>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<form action="inserir.php" method="post" target="_self">
    <label for="email">Titulo:</label><br>
    <input type="text" id="titulo" name="titulo" value="">
    <br>

    <label for="senha">Descrição:</label><br>
    <input type="text" id="descricao" name="descricao" value="">
    <br>
    <label for="senha">Preço:</label><br>
    <input type="text" id="preco" name="preco" value="">
    <br>
    <button type="submit" id="acao" name="acao" value="cadastrar">Cadastrar</button>
</form>
</body>
</html>

INSERT.PHP

    <?php 
try { 

$titulo = $_REQUEST['titulo'];
$descricao = $_REQUEST['descricao'];
$preco = $_REQUEST['preco'];

    $pdo = new PDO('mysql:host=localhost;dbname=diner', 'root', ''); 
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    $stmt = $pdo->prepare('INSERT INTO prato(titulo, descricao, preco) VALUES(:titulo, :descricao, :preco)'); 
    $stmt->execute(array( ':titulo' => '$titulo' )); 
    $stmt->execute(array( ':descricao' => '$descricao' )); 
    $stmt->execute(array( ':preco' => '$preco' )); 
    echo $stmt->rowCount();
     } catch(PDOException $e) { 
        echo 'Error: ' . $e->getMessage();
?>

From there I am no longer successful.. appears the following error: Error: SQLSTATE[HY093]: Invalid Parameter number: number of bound variables does not match number of tokens

my table is: DISH and has the columns: prato_id = int = Primary title = varnish Description = scan price = varchar I know I need to generate an ID number, because this will not be by the user, and I do not know how to do.

second, after recording this data, how do I make a list of the table printed in HTML ? showing the entries ?

and lastly, delete some id.

I’ve been running the internet for 2 days trying to learn about CRUD and PDO, but I stopped at it.

2 answers

9


SQLSTATE[HY093]: Invalid Parameter number: number of bound variables does not match number of tokens

It means that the number of parameters passed in the query is not equal to the number of columns or vice versa. In your code are made 3 queries that pass only one parameter.

$stmt->execute(array(':titulo' => '$titulo')); 
$stmt->execute(array(':descricao' => '$descricao')); 
$stmt->execute(array(':preco' => '$preco')); 

There are two ways to bind between the markers(named placeholders) and the values, the first is to specify each value individually with bindValue() or bindParam() the difference between these two methods is that in the first it is allowed to pass values directly while the second only accepts references(variables) return of function or method generate the error: Cannot pass parameter 2 by reference

Shape with bindValue/bindParam

$stmt = $pdo->prepare('INSERT INTO prato(titulo, descricao, preco) VALUES(:titulo, :descricao, :preco)'); 
$stmt->bindValue(':titulo', $titulo)); 
$stmt->bindValue(':descricao', $descricao)); 
$stmt->bindValue(':preco', $preco));
$stmt->execute();

Form with execute

With execute() it is possible to pass all the parameters at once through an array, it is quite useful in dynamic queries.

$stmt = $pdo->prepare('INSERT INTO prato(titulo, descricao, preco) VALUES(:titulo, :descricao, :preco)');
$stmt->execute(array(':titulo' => $titulo, ':descricao' => $descricao, ':preco' => $preco));
  • I can do this with select too? $stmt = $pdo->prepare('SELECT FROM prato VALUES(titulo, descricao, preco)');?> and print the result on the screen for a pagination? here I tried to use the ->select(); but the following error: Fatal error: Call to undefined method Conexao::select()

  • @Manzetti.Nis, no SELECT has not VALUES, the names of the fields go before the FROM, this error means that there is no method with that name in the Related class. It is best to open a new question.

4

I managed to solve it this way:

In the database, I changed the column id_prato for auto-increment.

the code went like this:

<?php 

Try {

$pdo = new PDO('mysql:host=localhost;dbname=diner', 'root', ''); 
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$pdo = $pdo->prepare('INSERT INTO prato VALUES(DEFAULT, :titulo, :descricao, :preco)'); //Preparando os dados
$pdo->bindValue( ':titulo' , $_REQUEST['titulo']); // recebendo dados do formulario
$pdo->bindValue( ':descricao' , $_REQUEST['descricao']); 
$pdo->bindValue( ':preco' , $_REQUEST['preco']); 
$pdo->execute(); // salvando no banco
echo $pdo->rowCount(); // retorna quantas linhas foram alteradas.
 } catch(PDOException $e) { 
    echo 'Error: ' . $e->getMessage();}

?>

Browser other questions tagged

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