2014-10-07 59 views
-2

下面是代碼檢查是否給出錯誤結果的語句?

<?php 
$username = $_POST['username']; 
$email = $_POST['email']; 
$password = $_POST['password']; 
$phone = $_POST['phone']; 
$referral = $_POST['refer']; 
$referred = false; 
mysql_connect("localhost","username","password") or die (mysql_error()); 
mysql_select_db("database") or die ("Cannot connect to database"); 
$query = mysql_query("Select * from member"); 
while($row = mysql_fetch_array($query)) 
{ 
    $table_users = $row['username']; 
    $table_email = $row['email']; 
    $table_phone = $row['phone']; 
    if($referral == $table_users) 
    { 
    $referred = true; 
    } 
    if($username == $table_users || $email == $table_email || $phone == $table_phone) 
    { 
    $bool = false; 
    } 
} 
if(($bool)) 
{ 
    $username = mysql_real_escape_string($username); 
    mysql_query("INSERT INTO member (username, password, email, phone, refer) VALUES ('$username', '$password', '$email', '$phone', '$referral')"); 
    if($referred) 
    { 
    $from="Sent from test"; 
    $subject="New user referred."; 
    $message="A new user " . $username . " has been referred by " . $referral . "Please stay updated. "; 
    mail("mymail", $subject, $message, $from); 
    } 
    $_SESSION['login'] = true; 
    echo "Thank you for registering with us.You can <a href=\"index.php\">login</a> now to start earning."; 
} 

如果推薦碼字段爲空或它不匹配數據庫中的任何值仍然發送 郵件。那麼,這裏發生了什麼?我添加了更多的代碼。我更早離開了它的一部分。

回答

0

此聲明if($referral == $table_users)看起來不正確。您尚未在代碼中的任何位置設置$referral變量。

+0

要添加到此,您應該將'if'語句更改爲'if(isset($ referral)&& $ referral == $ table_users){...}'。 – usandfriends 2014-10-07 01:36:03

+0

@ ٌٌٌٌٌٌّّّّّّ反正你仍然應該使用'isset'技術。這是一個很好的做法,因爲它減少了來自非集合'$ _POST []'的警告。 – usandfriends 2014-10-07 01:38:30

+0

你必須做更多的調試。查詢返回的行數和值是什麼?在if(($ bool))'語句之前做'var_dump($ bool);'來確認這個值是否爲真(如此)。 $ username,$ email或$ phone可以與數據庫中的行匹配。 – Lock 2014-10-07 01:40:44