2016-07-23 94 views
2

我試過用這個問題調試幾乎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()); 
    } 

?> 

我知道我忽略了確認密碼,但我要確保我可以插入到數據庫第一。

回答

0

解決方法:我的密碼長度在我的數據庫中爲15 ...散列密碼>> 15個字符。將長度改爲255,這樣很好。希望這對其他人有用。

+0

哦,這很糟糕。很高興你找到它 – BeetleJuice

相關問題