$ stmt - > execute()返回false

时间:2016-07-23 04:22:59

标签: php sql user-registration

我试过调试这个问题差不多4个小时没有运气。没有错误消息,$ stmt - > execute()因为我不知道的原因而给出错误。

register.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>

database.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());
    }

?>

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

1 个答案:

答案 0 :(得分:0)

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

相关问题