2017-02-21 42 views
0

我正在嘗試設置用戶註冊並使用PHP和MySQL登錄。我似乎已經到了它應該工作的地步,但是我實際上單擊「註冊」按鈕後,除了空白頁面之外,我正努力獲得其他任何東西。我知道數據庫連接正常,因爲更改我的'db.php'文件中的憑據會導致錯誤。我試圖讓PHP顯示錯誤,但無濟於事,仍然是空白頁面!用戶註冊表單提交後產生空白頁面

以下代碼中是否有任何可能導致問題的內容跳出?有錯誤處理代碼,無論註冊是否成功,都應該顯示錶單,但只顯示空白。

<html> 
<head> 
    <title>Registration</title> 
    <link rel="stylesheet" href="css/style.css" /> 
</head> 

<body> 

    <?php 

    require('db.php'); 

    if (isset($_REQUEST['username'])) // If form submitted, insert values into the database. 
    { 

     $username = stripslashes($_REQUEST['username']); // removes backslashes 
     $username = mysqli_real_escape_string($con,$username); //escapes special characters in a string 

     $email = stripslashes($_REQUEST['email']); 
     $email = mysqli_real_escape_string($con,$email); 

     $password = stripslashes($_REQUEST['password']); 
     $password = mysqli_real_escape_string($con,$password); 

     $query = "INSERT INTO users (username, password, email) VALUES ('$username', '".md5($password)."', '$email')"; 

     $result = mysqli_query($con,$query); 

     echo $result; 

     if($result) { 
      echo "<div class='form'><h3>You are registered successfully.</h3><br>Click here to <a href='login.php'>Login</a></div>"; 
     } 
    } else { 

    ?> 
     <div class="form"> 
      <h1>Registration</h1> 
      <form name="registration" action="" method="post"> 
       <input type="text" name="username" placeholder="Username" required /> 
       <input type="email" name="email" placeholder="Email" required /> 
       <input type="password" name="password" placeholder="Password" required /> 
       <input type="submit" name="submit" value="Register" /> 
      </form> 
     </div> 
    <?php } ?> 

</body> 

+1

一)字符串轉義不能完全保護您不受SQL注入 - 使用使用**參數化的語句** B) unsalted'md5'用於密碼哈希與存儲純文本密碼一樣糟糕 - 使用'password_hash()'和'password_verify()'c)嘗試'var_dump($ result)' - 'echo'不會打印任何東西虛假 –

+0

添加'ini_set('display_errors',1);函數ini_set( 'log_errors',1);使用error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);'到腳本的頂部。這將強制任何'mysqli_'錯誤到 生成一個你不能錯過或忽略的異常。 – RiggsFolly

+0

這實際上是我第一次看到做這樣的事情。希望在應用最佳實踐之前獲得不安全的版本。但是,我正在努力爭取到這一點。 – LegendEater

回答

0

多虧了RiggsFolly給我一些錯誤報告,我設法看到,對於電子郵件地址列名是不正確的。解決這個問題解決了我的問題。

方便的提示。謝謝。

0

這是錯誤的。

if (isset($_REQUEST['username'])) 

您正在提交表格,並且應該有REQUEST變量的表格名稱。而是執行此

,而應該是

if (isset($_REQUEST['registration'])) 

或者

if (isset($_POST['submit'])) 
{ 
    $username = stripslashes($_POST['username']); 

    $email = stripslashes($_POST['email']); 

    $password = stripslashes($_POST['password']); 
+0

它也適用於我的方式。你的方式有什麼好處嗎? – LegendEater

+0

是的,實際上你可以通過這個窗體已經提交的信息來告訴'isset($ _ POST ['submit'])'!它可以確保您的表單已正確提交。 –