A few days ago I started working with 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 not with user, now when I added that variable I get the error of
"Warning: PDOStament::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\2018\Register2\signup.php on line 11"
and I already analyzed my code and I can't find the problem, my signup.php code 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());
}
?>
FINALLY WITH THE COMMENTS I MAKE YOU, YOUR CODE SHOULD BE AS FOLLOWS
Of course, when you execute the insertion, you put the parameters and then put the values as they are written. Here is an example code: