2013-12-10 99 views
1

我在努力使事情變得簡單。當我輸入一個現有的電子郵件和錯誤的密碼都顯示錯誤。當我輸入錯誤的電子郵件地址和正確的密碼格式時,顯示電子郵件的錯誤消息,但是當我輸入錯誤的密碼和正確的電子郵件時,兩個錯誤消息都會顯示出來,而且應該只輸入錯誤的密碼。我只是一個初學者,我想知道這是如何工作的,但我在這裏找不到解決方案。我應該爲陣列使用不同的名稱嗎? 這裏是我的代碼:Php mysql錯誤信息

<?php 
session_start(); 
$errmsg_arr = array(); 
$errflag = false; 
//$errmsg_arr2 = array(); 
//$errflag2 = false; 
include('config.php'); 


$firstname=$_POST['firstname']; 
$lastname=$_POST['lastname']; 
$email=$_POST['email']; 
$pword=$_POST['pword']; 
$number=$_POST['number']; 
$house=$_POST['house']; 
$street=$_POST['street']; 
$city=$_POST['city']; 

$min_length = 6; 
    // you can set minimum length of the query if you want 
$result = mysql_query("select 1 from athan_members where email='" 
         . mysql_real_escape_string($email) . "'"); 
$userExists = (mysql_fetch_array($result) !== FALSE); 
mysql_free_result($result); 

if ($userExists = true){ 
$errmsg_arr[] = 'email address is already used'; 
$errflag = true; 
} 
if(strlen($pword)< $min_length){ 
$errmsg_arr[] = 'password must contain not less than 6 characters'; 
$errflag = true; 
} 
else{ 
mysql_query("INSERT INTO athan_members (firstname, lastname, email, number, house1,  street1, city, password) VALUES ('$firstname', '$lastname', '$email', '$number', '$house',  '$street', '$city', '$pword')"); 
header("location: loginuser.php"); 
} 

/*if(strlen($pword) >= $min_length){ 
//this one will not feed in the database if there's a duplicate but still a problem ohmaygawd:3 
//mysql_query("INSERT INTO athan_members (firstname, lastname, email, number, house1,  street1, city, password) VALUES ('$firstname', '$lastname', '$email', '$number', '$house',  '$street', '$city', '$pword') ON DUPLICATE KEY UPDATE") 
mysql_query("INSERT INTO athan_members (firstname, lastname, email, number, house1,  street1, city, password) VALUES ('$firstname', '$lastname', '$email', '$number', '$house',  '$street', '$city', '$pword')"); 
header("location: loginuser.php"); 
} 
else 
{ 
$errmsg_arr[] = 'password must contain not less than 6 characters'; 
$errflag = true; 
}*/ 


if ($errflag) { 
     $_SESSION['ERRMSG_ARR'] = $errmsg_arr; 
     session_write_close(); 
     header("location: new.php"); 
     exit(); 
} 
mysql_close($con); 
?> 
+0

你需要在調用頭文件後調用'exit()',並且在執行SQL語句之前需要清理變量。 –

回答

3

您有:

if ($userExists = true)... 

將分配值$ userExists沒有比較和該條款將永遠是真(往那個部分)。

您應該比較的是,而不是像這樣:

if ($userExists == true) { 

    // exists 
} else { 

    // not 
} 

和更改這個密碼校驗碼:

if(strlen($pword)< $min_length){ 
$errmsg_arr[] = 'password must contain not less than 6 characters'; 
$errflag = true; 
} 
else{ 
mysql_query("INSERT INTO athan_members (firstname, lastname, email, number, house1,  street1, city, password) VALUES ('$firstname', '$lastname', '$email', '$number', '$house',  '$street', '$city', '$pword')"); 
header("location: loginuser.php"); 
} 

這樣:

​​

你只檢查是否密碼太短。不是如果電子郵件已經存在這會檢查兩者。

+1

甚至'if($ userExists === true)' – meda

+0

@meda如果變量類型是布爾型(相同變量類型) – Hardy

+1

此工作先生@Hardy。真的救了我的生命:) – user3088818