数据库插入不起作用(MySQL,PHP)

时间:2016-07-16 19:12:24

标签: php mysql

我有这个PHP,基本上用于将电子邮件和密码插入SQL数据库:

<?php  
error_reporting(E_ALL ^ E_STRICT);
require "database.php";
$message = '';

  if (!empty($_POST["email"]) &&!empty($_POST["password"])):
  //Enter the new user in the database
    $sql = "INSERT INTO users (email, password) VALUES (:email, :password)";
    $stmt = $conn->prepare($sql);

    $stmt->bindParam(":email", $_POST['email']);
    $stmt->bindParam(":password", password_hash($_POST['password'], PASSWORD_BCRYPT));

    if ($stmt->execute() ):

      $message = 'Successfully created a new user';
    else:
      $message = 'Sorry there must have been an issue whilst registering';
    endif;

  endif;
?>

以下是表格:

<div class="jumbotron" id="jumbotron-6">
  <div class="container text-center">
  <?php if (!empty($message)):
  ?>
  <h3 id="h3message"><?= $message ?> </h3>
<?php endif; ?>
     <form action="signup.php" method="POST">
      <input type="text" placeholder="enter your email" name="email"> 
        <input type="password" placeholder="and password" name="password">
          <input type="password" placeholder="confirm password" name="confirm_password">
        <input type="submit">
    </form> 
  </div>
</div>

它没有插入到数据库中(所有字段,变量都是正确的,我认为 - 只是电子邮件和密码)然后它会返回我创建的错误消息,上面写着'抱歉,注册时一定有问题“

这是database.php文件

<?php 

$server =  'localhost';
$username = "root";
$password = "";
$database = "auth";

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

?>

1 个答案:

答案 0 :(得分:0)

在绑定密码之前散列密码:

$UserPWHash = password_hash($_POST['password'], PASSWORD_BCRYPT);

$stmt->bindParam(":password", $UserPWHash));