使用PHP检查MySQL中的用户名和密码

时间:2015-05-25 10:22:49

标签: php mysql login

我几个小时都在寻找答案,但没有发现任何有效的方法。我正在尝试建立一个网站,要求您注册并登录才能访问该网站。我有注册工作。但是当我尝试登录时,我似乎无法实际检查用户是否已经存在。数据库只有2列。

    <?php 
session_start();

$username = $_POST['usernameS'];
$password = $_POST['userPassS'];
$mysql_db_hostname = "localhost";
$mysql_db_user = "root";
$mysql_db_password = "";
$mysql_db_database = "sugoidb";
$con = mysqli_connect($mysql_db_hostname, $mysql_db_user, $mysql_db_password)
or die("Could not connect database " .mysqli_error($con));

if (!$con)
{
  echo "Please try later.";
}
else
{
mysqli_select_db($con, $mysql_db_database);
}

$query = "SELECT * FROM users WHERE 'Username'='$username' AND 'Paaword'='$password'" or die("Could not connect database " .mysqli_error($con));

$result = mysqli_query($con, $query);

if (!$result) {
    printf("Error: %s\n", mysqli_error($con));
    exit();
}

// Mysql_num_row is counting table row
$count=mysqli_num_rows($result);
// If result matched $username and $password, table row must be 1 row
if($count==1){
    $row = mysqli_fetch_assoc($result);
    if (crypt($password, $row['userPassS']) == $row['userPassS']){
        session_register("username");
        session_register("password"); 
        echo "Login Successful";
        return true;
    }
    else {
        echo "Wrong Username or Password";
        return false;
    }
}
else{
    echo "Wrong Username or Password";
    return false;
}


mysqli_close($con)
?>

2 个答案:

答案 0 :(得分:2)

您的查询错误:

$query = "SELECT * FROM users WHERE Username='$username' AND Paaword='$password'";
$result = mysqli_query($con, $query)  or die("Could not connect database " .mysqli_error($con));

答案 1 :(得分:0)

只需删除$ sql左侧的单引号,例如Username='$username' AND Paaword='$password',并检查$ sql中的表名称是“Paaword”还是密码。