几天前,我开始在本地服务器上使用 MySQL 数据库,目前一切顺利,直到我添加了用户变量,我只使用电子邮件而不是用户,现在当我添加该变量时,我得到了的错误
“警告:PDOStament::execute(): SQLSTATE[HY093]: 无效的参数号:绑定变量的数量与第 11 行 C:\xampp\htdocs\2018\Register2\signup.php 中的标记数量不匹配”
我已经分析了我的代码,但我找不到问题,我的 signup.php 代码是
<?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>
database.php 是这样的:
<?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());
}
?>
最后加上我给你的评论,你的代码应该如下
当然,当你执行插入的时候,你把参数放好,然后把它们写的值放好。这是一个示例代码: