登录PHP时连接失败

时间:2015-05-05 17:55:31

标签: php localhost login-script

我的任何一部分都不了解此代码的问题!它确实有效,现在它没有。我无法登录'个人资料页面'任何人都可以看到和发出?

<?php
session_start(); 

$error=''; 
if (isset($_POST['submit'])) 
{
    if (empty($_POST['username']) || empty($_POST['password'])) 
    {
        $error = "Enter username & password";
    }
else {
    // Define $username and $password
    $username=$_POST['username'];
    $password=$_POST['password'];

    $connection = mysql_connect("localhost", "root", "");
    echo($connection);
    $username = stripslashes($username);
    $password = stripslashes($password);
    $username = mysql_real_escape_string($username);
    $password = mysql_real_escape_string($password);

    $db = mysql_select_db("Windsor", $connection);
        echo($db);      
        $sql = "SELECT * FROM PHP_Customer WHERE Email='$username' and Password='$password'";
        echo($sql);
        $result = mysql_query($sql);

            $count=mysql_num_rows($result);

            if ($count < 1) 
            {
                $error = "Username or Password is incorrect";
            }
            else
            {
                $_SESSION['user_login'] = $username; 
                header('location: profile.php');
                echo "hello world!"; // This is just for testing purposes

            }
        } 
mysql_close($connection); 
}

?>

正如我所说,它确实起作用,无论出于什么原因它似乎不再存在。

1 个答案:

答案 0 :(得分:0)

好的发现了几个问题。

1)声明中有一个小写的“AND”。它应该是:

 $sql = "SELECT * FROM PHP_Customer WHERE Email='$username' AND Password='$password'";

2)不要将mysql_select_db设置为变量并回显它。该行应为:

mysql_select_db("Windsor", $connection);

3)不要回显连接细节。

4)在mysql语句中添加一些错误检查。

该脚本现在应如下所示:

<?php
session_start(); 

$error=''; 
if (isset($_POST['submit'])) 
{
    if (empty($_POST['username']) || empty($_POST['password'])) 
    {
        $error = "Enter username & password";
    }
else {
    // Define $username and $password
    $username=$_POST['username'];
    $password=$_POST['password'];

    $connection = mysql_connect("localhost", "root", "") or die(mysql_error());
    $username = stripslashes($username);
    $password = stripslashes($password);
    $username = mysql_real_escape_string($username);
    $password = mysql_real_escape_string($password);

    mysql_select_db("Windsor", $connection) or die(mysql_error());      
        $sql = "SELECT * FROM PHP_Customer WHERE Email='$username' AND Password='$password'";
        $result = mysql_query($sql) or die(mysql_error());

            $count=mysql_num_rows($result);

            if ($count < 1) 
            {
                $error = "Username or Password is incorrect";
            }
            else
            {
                $_SESSION['user_login'] = $username; 
                header('location: profile.php');
                echo "hello world!"; // This is just for testing purposes

            }
        } 
mysql_close($connection); 
}

?>