2015-11-03 82 views
0

這可能是有史以來最簡單的錯誤,但我寫了一個註冊腳本..我會說,看起來好吧..唯一的問題是,它不會插入數據...它仍然打印一條消息說註冊成功,但沒有實際的數據進入數據庫......看到下面的代碼:PHP註冊腳本 - 插入數據

<?php 

include("dbconfig.php"); 

if(isset($_POST['register'])){ 

if(empty($_POST['first-name']) or empty($_POST['last-name']) or empty($_POST['email-address']) or empty($_POST['reg-username']) or empty($_POST['reg-pass'])){ 

    header("location:index-login-page.php?msg0=Please complete the required fields."); 


} 

else { 

    $fname = $_POST['first-name']; 
    $lname = $_POST['last-name']; 
    $email = $_POST['email-address']; 
    $username = $_POST['reg-username']; 
    $pass = $_POST['reg-pass']; 

    $checkusername = mysql_query("SELECT username FROM users WHERE username = '$username'"); 
    $checkemail = mysql_query("SELECT email FROM users WHERE email = '$email'"); 
    $resultusername = mysql_num_rows($checkusername); 
    $resultemail = mysql_num_rows($checkemail); 

    if((($resultusername) ==1) or ($resultemail)==1){ 
     header("location:index-login-page.php?msg1= Username or email address already exists."); 

    } 

    elseif((($resultusername) == 0) && ($resultemail) ==0) { 

     $insertquery =("INSERT INTO users (firstname, lastname, email, username, password) VALUES ('$fname','$lname','$email','$username','$pass'"); 
      header("location:index-login-page.php?msg1= Registration successful, please login."); 

    } 

} 

} 

?> 

請不要讓我知道是什麼錯誤(如果有的話),因爲我似乎無法找到它。謝謝。

蘇海爾。

回答

2
$insertquery = ("INSERT INTO users (firstname, lastname, email, username, password) VALUES ('$fname','$lname','$email','$username','$pass'"); 

應該是:

$insertquery = mysql_query("INSERT INTO users (firstname, lastname, email, username, password) VALUES ('$fname','$lname','$email','$username','$pass'"); 

我不得不雖然警告你:這被認爲是不好的做法,你需要清理你的數據庫輸入

+0

現在我會嘗試這一點,但你是什麼「淨化」意味着什麼? –

+0

請閱讀:http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php/1669109#1669109 – JorisK

+0

謝謝先生,我看了看,我可以向你保證,我將考慮未來的發展。現在,我只是在修改我的記憶,因爲我已經做了7個月的編碼。謝謝。 –