php - $stmt -> execute() 返回 false

标签 php sql user-registration

我已经尝试调试这个问题将近 4 个小时,但没有成功。没有错误消息,$stmt -> execute() 出于我不知道的任何原因给出 false。

注册.php

<?php

session_start();

if( isset($_SESSION['id'])){
  header("Location: index.php");
}

require 'database.php';

$message = '';

if(!empty($_POST['user']) && !empty($_POST['password'])):

    $pass = $_POST['password'];
    $user = $_POST['user'];

    $sql = "Insert into user (username, password) values (:user, :password)";
    $stmt = $conn->prepare($sql);

    $stmt->bindValue(':user', $_POST['user']);
    $stmt->bindValue(':password', password_hash($_POST['password'], PASSWORD_BCRYPT));

    //var_dump($_POST['password']);
    //var_dump($_POST['user']);

    if($stmt -> execute()):
      $message = 'Successfully created new user';
    else:
      $message = 'Sorry there must have been an issue creating your account';
    endif;


endif;

?>

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Register Below</title>
  </head>
  <body>

    <?php if(!empty($message)): ?>
      <p><?= $message ?></p>
    <?php endif; ?>

    <h1>Register</h1>
    <form class="" action="register.php" method="post">

      <input type="text" placeholder="Username" name="user" id="user">
      <input type="password" placeholder="and password" name="password" id="password">
      <input type="password" placeholder="confirm password" name="confirm_password">

      <button type="submit" name="button">Register</button>
    </form>
    <a href="./index.php">home</a>
  </body>
</html>

数据库.php

<?php

  $server = 'localhost';
  $username ='master';
  $password ='master';
  $database = 'quiz';

    try{
        $conn = new PDO("mysql:host=$server;dbname=$database;" , $username, $password);
    } catch(PDOException $e){
        die ("Connection failed" . $e->getMessage());
    }

?>

我知道我忽略了确认密码,但我想确保我可以先插入数据库。

最佳答案

解决方案:我的数据库中的密码长度为 15...散列密码 >> 15 个字符。将长度更改为 255,一切都很好。希望这对其他人有用。

关于php - $stmt -> execute() 返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38538136/

相关文章:

php - 从 SQL DB 添加和/或删除记录

c# - 如何使用 DataTable.Select() 解决文件管理器值中的单引号问题

mysql - 对具有相同列值的值进行分组/求和

ajax - 如何安全地检查用户名是否已被占用?

php - PDOStatement 执行不使用 Mysql-ODBC 填充值

php - 我可以连接来自不同数据库的两个表吗?

ruby-on-rails - 如何创建 Controller 和 View 以使用 Devise gem 作为身份验证机制来显示用户的个人资料?

java - 从异常重定向回来后如何将数据保留在表单中

php - 计算两个小数之间的百分比增加

php - 绑定(bind)准备好的结果不返回输出