2017-03-28 73 views
-1

我想允許用戶從兩個表使用一種形式登錄,檢查第一個表,如果用戶存在,如果它不存在它應該檢查其他表回聲用戶確實存在。我曾嘗試以下,但它不工作允許用戶登錄使用不同的表中的數據

<?php$username = $DBcon->real_escape_string($username); 
$email = $DBcon->real_escape_string($email); 
$password = $DBcon->real_escape_string($password); 
$hash = password_hash($password, PASSWORD_DEFAULT); 
$get = "SELECT _userid, u_name, hemail, pass_word from five_thousand where hemail='$email' or u_name='$username' limit 1"; 
$get2 = "SELECT _userid, u_name, hemail, pass_word from ten_thousand where hemail='$email' or u_name='$username' limit 1"; 
// for five thousand 
$drup = $DBcon->query($get); 
$row=$drup->fetch_array(); 

// for ten thousand 
$drup2 = $DBcon->query($get2); 
$row2 = $drup2->fetch_array(); 

if ($row['hemail'==1]) { 
    if ($row['pass_word'] == $hash) { 
    $_SESSION['userSession'] = $row['user_id']; 
    header("Location: ../office.php"); 
}else { 
     $msg = "<div class='alert alert-danger'> 
        <span class='glyphicon glyphicon-info-sign'></span> &nbsp; Invalid Username or Password ! 
       </div>"; 
    } 
    } 
elseif ($row2['hemail']==1) { 
    if ($row['pass_word'] == $hash) { 
    $_SESSION['userSession'] = $row['user_id']; 
    header("Location: ../office.php"); 
    }else { 
     $msg = "<div class='alert alert-danger'> 
        <span class='glyphicon glyphicon-info-sign'></span> &nbsp; Invalid Username or Password ! 
       </div>"; 
    } 
}else{ 
    echo "user does not exixt"; 
} 
$DBcon->close(); 
?> 

回答

0

我會做這樣的事情,而不是

$username = $DBcon->real_escape_string($username); 
$email = $DBcon->real_escape_string($email); 
$password = $DBcon->real_escape_string($password); 
$hash  = password_hash($password, PASSWORD_DEFAULT); 
$tables = array('five_thousand', 'ten_thousand'); 

function getData($table, $username, $email, $password, $DBcon) { 
    $query = "SELECT _userid, u_name, hemail, pass_word from $table where hemail='$email' or u_name='$username' limit 1"; 
    $drup = $DBcon->query($query); 
    $row = $drup->fetch_array(); 

    return $row['hemail']; 
} 

if ($row = getData('five_thousand', $username, $email, $password, $DBcon)) { 
    if ($row['pass_word'] == $hash) { 
     $_SESSION['userSession'] = $row['user_id']; 
     header("Location: ../office.php"); 
    } else { 
     $msg = "<div class='alert alert-danger'> 
       <span class='glyphicon glyphicon-info-sign'></span> &nbsp; Invalid Username or Password ! 
      </div>"; 
    } 
} else { 
    echo "user does not exixt"; 
} 

if ($row = getData('ten_thousand', $username, $email, $password, $DBcon)) { 
    if ($row['pass_word'] == $hash) { 
     $_SESSION['userSession'] = $row['user_id']; 
     header("Location: ../office.php"); 
    } else { 
     $msg = "<div class='alert alert-danger'> 
       <span class='glyphicon glyphicon-info-sign'></span> &nbsp; Invalid Username or Password ! 
      </div>"; 
    } 
} else { 
    echo "user does not exixt"; 
} 

$DBcon->close(); 
相關問題