2016-06-28 39 views
-2

我正在學習php和mysql,並嘗試使用註冊和登錄頁面創建表單,但是我無法獲取將數據寫入數據庫的註冊表單。我不會在連接數據庫時遇到錯誤,但是當我嘗試發佈數據時,表格仍爲空。我得到錯誤使用PHP和MySQL登記表

'註冊時出現問題。請稍後再試。

任何幫助,非常感謝。

<?php 
//signup.php 
include 'connect.php'; 

echo '<h2>Register </h2>'; 
if($_SERVER['REQUEST_METHOD'] != 'POST') 
{ 
    /*the form hasn't been posted yet, display it 
     note that the action="" will cause the form to post to the same page it is on */ 
    echo '<form method="post" action=""> 
     Username: <input type="text" name="Username" /> 
     Password: <input type="password" name="Password"> 
     Confirm Password: <input type="password" name="Confirm"> 

     <input type="submit" value="Submit" /> 
    </form>'; 

    } 
else 
{ 
    /* so, the form has been posted, we'll process the data in three steps: 
     1. Check the data 
     2. Let the user refill the wrong fields (if necessary) 
     3. Save the data 
    */ 
    $errors = array(); /* declare the array for later use */ 

    if(isset($_POST['Username'])) 
    { 
     //the user name exists 
     //if(!ctype_alnum($_POST['Username'])) 
     if($_POST['Username'] == ['Username']) 
     { 
      $errors[] = 'The username is already in use.'; 
     } 

     } 
    else 
    { 
     $errors[] = 'The username field must not be empty.'; 
    } 

    if(isset($_POST['Password'])) 
    { 
     if($_POST['Password'] != $_POST['Confirm']) 
     { 
      $errors[] = 'The two passwords did not match.'; 
     } 
    } 
    else 
    { 
     $errors[] = 'The password field cannot be empty.'; 
    } 

    if(!empty($errors)) /*check for an empty array, if there are errors, they're in this array (note the ! operator)*/ 
    { 
     echo 'Uh-oh.. a couple of fields are not filled in correctly..'; 
     echo '<ul>'; 
     foreach($errors as $key => $value) /* walk through the array so all the errors get displayed */ 
     { 
      echo '<li>' . $value . '</li>'; /* this generates a nice error list */ 
     } 
     echo '</ul>'; 
    } 
    else 
    { 
     //the form has been posted without errors, so save it 
     //notice the use of mysql_real_escape_string, keep everything safe! 
     //also notice the sha1 function which hashes the password 
     $sql = "INSERT INTO 
        Users(Username, Password) 
       VALUES('" . mysql_real_escape_string($_POST['Username']) . "', 
         '" . md5($_POST['Password']) . "', 
         NOW(), 
         0)"; 

     $result = mysql_query($sql); 
     if(!$result) 
     { 
      //something went wrong, display the error 
      echo 'Something went wrong while registering. Please try again later.'; 
      //echo mysql_error(); //debugging purposes, uncomment when needed 
     } 
     else 
     { 
     header ("location: index.htm"); //redirects to Index Page 


     } 
    } 
} 

?> 

謝謝

+0

嘗試手動插入查詢 – LOKESH

+2

簡單:您不檢查錯誤,Lord只知道'connect.php '。 –

+2

你正在告訴腳本只在用戶(用戶名,密碼)中插入,但是你也在解析NOW()和0) – b0ne

回答

1

試試這個。我已經加入反勾到Users表中的字段

$username = mysql_real_escape_string($_POST['Username']); 
    $password = md5($_POST['Password']); 


    $sql= "INSERT INTO Users(`Username`,`Password`) VALUES('$username', '$password')"; 

要插入NOW()和0.2額外值

注意,在調試SQL查詢的第一步是運行它在MySQL第一。因此,請先嚐試在MySQL中首先運行SQL語句,其虛擬值爲UsernamePassword,並查看它是否有效

+0

謝謝Kev,這個工作,我現在可以前進! – DarkKnight1203

+0

如何獲取用於檢查用戶名的代碼?使用mysqli或php更好嗎? – DarkKnight1203

+0

我會建議你用戶mysqli而不是從一開始的MySQL。若要檢查用戶是否存在,請使用mysql語句'SELECT COUNT(user_id)FROM Users WHERE'username' ='$ username''如果結果大於0,則用戶存在,否則結果爲0沒有用戶退出。其實,正確的邏輯是,如果有一個用戶使用該用戶名,那麼結果應該是1,而不是更多 – theTypan