error with MySQL: Invalid parameter number: number of bound variables does not match number of tokens

1

A few days ago I started working with the MySQL databases on my local server, for the moment it was going well until I added the user variable, I had only done it with email but with no user, now when I added that variable I miss the error of

  

"Warning: PDOStatement :: execute (): SQLSTATE [HY093]: Invalid parameter   number: number of bound variables does not match number of tokens in   C: \ xampp \ htdocs \ 2018 \ Registration2 \ signup.php on line 11 "

and already analyze my code and I can not find the problem, my code of signup.php is

<?php 
    require 'database.php';
    $message = '';
    if (!empty($_POST['usuario']) && !empty($_POST['password'])) {
        $sql = "INSERT INTO usuarios (usuario, email, password) VALUES (:usuario, :email, :password)";
        $stmt = $conn -> prepare($sql);
        $stmt -> bindParam(':usuario',$_POST['usuario']);
        $password = password_hash($_POST['password'], PASSWORD_BCRYPT);
        $stmt -> bindParam(':password',$_POST['password']);

        if ($stmt -> execute()) {
            $message = 'Se ha creado su cuenta satisfactoriamente en la pagina';
        }
        else {
            $message = 'Lo sentimos, pero ha ocurrido un problema con la base de datos';
        }
    }
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Registrarse</title>
    <link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
    <?php require 'partials/header.php' ?>
    <?php if (!empty($message)):
    ?>
        <p><?= $message ?></p>
    <?php
    endif; 
    ?>
    <h1>Registrarse</h1>
    <span>ó <a href="login.php">Iniciar Sesión</a></span>
    <form action="signup.php" method="POST">
        <input type="text" name="usuario" placeholder="Usuario">
        <input type="text" name="email" placeholder="Email">
        <input type="password" name="password" placeholder="Contraseña">
        <input type="password" name="confirm_password" placeholder="Confirme la contraseña">
        <input type="submit" value="Enviar">
    </form>
</body>
</html>

and the database.php is this:

<?php
$server = 'localhost';
$username = 'root';
$password = '';
$database = 'usuarios';
try {
  $conn = new PDO("mysql:host=$server;dbname=$database;", $username, $password);
} catch (PDOException $e) {
  die('Connection Failed: ' . $e->getMessage());
}
?>
    
asked by Luis Felipe 28.02.2018 в 00:45
source

2 answers

2
  

The number of values that you send in the form of variables should be   correspond to the number of columns that you plan to fill in your table   your database, which does not happen because you need the attribute    mail

     

That's why it tells you that the number of tokens does not tie, because the   number of variables does not correspond to the number of columns

FINALLY WITH THE COMMENTS I MAKE YOU, YOUR CODE SHOULD REMAIN IN THE FOLLOWING MODE

<?php 
    require 'database.php';
    $message = '';
    if (!empty($_POST['usuario']) && !empty($_POST['password'])) {
        $sql = "INSERT INTO usuarios (usuario, email, password) VALUES (:usuario, :email, :password)";
        $stmt = $conn -> prepare($sql);
        $stmt -> bindParam(':usuario',$_POST['usuario']);
        $stmt -> bindParam(':email',$_POST['email']);
        $password = password_hash($_POST['password'], PASSWORD_BCRYPT);
        $stmt -> bindParam(':password',$_POST['password']);

        if ($stmt -> execute()) {
            $message = 'Se ha creado su cuenta satisfactoriamente en la pagina';
        }
        else {
            $message = 'Lo sentimos, pero ha ocurrido un problema con la base de datos';
        }
    }
?>
    
answered by 28.02.2018 / 00:51
source
1

My good friend the same number of columns in your table should be the same as you register, as the friend Alfredo Paz says you are missing the email variable, you are requesting it in your mysql query but you are not passing it in your variables . Remove it from the insert or add it in your post. Greetings

    
answered by 28.02.2018 в 01:05