2015-07-11 91 views
0

在我的網站上我有一個註冊表。用戶只需輸入電子郵件,密碼並重新輸入密碼即可。PHP不提交用戶的詳細信息到數據庫?

但是,即使填寫了所有字段,我仍然收到一個錯誤,表示我沒有填寫表格。我不明白我做錯了什麼(PHP相對較新)

請注意我沒有嘗試清理用戶輸入,因此我知道我的代碼將容易受到SQL注入!

<?php 
include("database.php"); 
include("requirements.php"); 

if(isset($_POST['register'])) { 
$email = $_POST['email']; 
$password = $_POST['password']; 
$confpassword = $_POST['confpassword']; 
} 

if(isset($password) && isset($email) && isset($confpassword)){ 
if($password != '' && $email != '' && $confpassword != ''){ 
    if($password == $confpassword){ 
     if(strlen($password) <= 20 && strlen($password) >= 6){ 
      if(strlen($email) > 6){ 
       $hash = password_hash($password, PASSWORD_DEFAULT);    
       $insert_user = mysqli_query($con, "INSERT INTO users (email, password, salt) VALUES ('".$email."', '".$password."', '".$salt."')"); 
        if($insert_user) 
        { 
        echo "Thank you for registering, you can now login to your account and create a listing."; 
        } 
        else 
        { 
        echo "Sorry there was an error - please try again later. If the issue continues please contact support."; 
        } 
       } 
       else 
       { 
       echo "You need to insert an email."; 
       } 
      } 
      else 
      { 
      echo "The entered email address needs to be above 6 characters."; 
      } 
     } 
     else 
     { 
     echo "The password must be between 6 and 20 characters long."; 
     } 
    } 
    else 
    { 
    echo "The two passwords you have entered do not match."; 
    } 
} 
else 
{ 
echo "You have not inserted all of the required fields that were needed."; 
} 

它只是跳到最後說「你沒有插入所有需要的字段。」甚至不嘗試查詢數據庫。

database.php中只是有數據庫信息(表,密碼等)

感謝您的連接!

回答

0

順便說一句,我做你的代碼的副本,並把它稱爲使用:

<form action="test.php" method="post"> 
    <input type="text" name="email" /> 
    <input type="text" name="password" /> 
    <input type="text" name="confpassword" /> 
    <input type="submit" name="register" /> 
</form> 

它完美。我認爲你的錯誤是在你的HTML表單中。

+0

原來,這是HTML - 在我的表單中,我的「Value =」註冊「」在我的按鈕上,而不是「佔位符=」註冊「」......菜鳥錯誤。多謝你們! – PublicDisplayName

+0

不客氣;) – Lemurantin

0

爲了理解發生了什麼,您應該在代碼的開始處使用「var_dump($ variable)」函數。 這是一個調試功能,顯示變量的內容(對數組有用)。

1)如果只有一個字段爲空,則該字段的名稱在某處存在問題。

2)如果所有的字段都是空的,你的表單或你的提交按鈕有問題。

只是一個提示:不要使用「isset」,然後檢查你的字段填寫,您可以使用「空($變量)!」,但它確實exactely同樣,在一個函數調用;)

狐猴

+0

沒有任何字段是空的,我已經正確填寫了所有字段,我不明白我出錯的地方。我將添加var_dump($變量) - 謝謝!編輯:如果我將兩個密碼字段留空,我會得到「您輸入的密碼不匹配」 – PublicDisplayName

1

我會嘗試手動將值分配給PHP變量($ email,$ password,$ confpassword)。

if(isset($_POST['register'])) { 
$email = $_POST['email']; 
$password = $_POST['password']; 
$confpassword = $_POST['confpassword']; 
} 

//Test variables 
$email = '[email protected]'; 
$password = 'testpassword'; 
$confpassword = 'testpassword'; 

if(isset($password) && isset($email) && isset($confpassword)){ 
if($password != '' && $email != '' && $confpassword != ''){ 

一旦您提交表單,如果工作正常,您知道問題出在您的$ _POST數據被分配給PHP變量。如果這不起作用,你知道你的邏輯存在一個問題,以及它如何處理這些變量。這至少會縮小你需要看的地方。

+0

好的,問題出在將$ _POST數據分配給PHP變量。感謝你 - 我如何進一步縮小問題的範圍? – PublicDisplayName

相關問題