允许用户登录使用来自不同表的数据

时间:2017-03-28 15:53:21

标签: php sql

我试图允许用户使用一个表单从两个表登录,如果用户存在则检查第一个表,如果不存在,则应检查另一个表,否则echo用户确实存在。我试过以下但是没有用

<?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();
?>

1 个答案:

答案 0 :(得分: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();