-2

我是 PHP 新手,所以请耐心等待!我正在尝试设置用户登录页面,但每次单击登录时,它都无法识别数据库中已有的数据。我目前在表格中有 7 个部分,但只从 2 个部分中获取数据。我不确定我哪里出错了可能是 php 或 MySQL 查询有人请帮助我!

 <?PHP

$email = "";
$pword = "";
$errorMessage = "";
$num_rows = 0;

function quote_smart($value, $handle) {

   if (get_magic_quotes_gpc()) {
       $value = stripslashes($value);
   }

   if (!is_numeric($value)) {
       $value = "'" . mysql_real_escape_string($value, $handle) . "'";
   }
   return $value;
}

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    $email = $_POST['Email'];
    $pword = $_POST['Password'];

    $email = htmlspecialchars($email);
    $pword = htmlspecialchars($pword);

    $e_mail = "root";
    $pass_word = "";
    $database = "the_travel_cult";
    $server = "127.0.0.1";

    $db_handle = mysql_connect($server, $e_mail, $pass_word);
    $db_found = mysql_select_db($database, $db_handle);

    if ($db_found) {

        $email = quote_smart($email, $db_handle);
        $pword = quote_smart($pword, $db_handle);


        $SQL = "SELECT * FROM user_login WHERE Email='$email' AND Password='$pword'";
        $result = mysql_query($SQL);
        $num_rows = mysql_num_rows($result);
        //if(!$result) die ('Unable to run query:'.mysql_error());

        if ($result) {
            if ($num_rows > 0) {
                session_start();
                $_SESSION['user_login'] = "1";
                header ("Location: SignedIn.php");
            }
            else {
                session_start();
                $_SESSION['user_login'] = "";
                //$errorMessage = "Not Registered";
                header ("Location: Register.php");              
            }   
        }
        else {
            $errorMessage = "Error logging on";
        }

    mysql_close($db_handle);

    }

    else {
        $errorMessage = "Error logging on";
    }
}


?>

<FORM NAME ="form1" METHOD ="POST" ACTION ="HomePage.php">

<form method=post action=”login.php”&gt;

<p><center><strong>Email Addres:</strong></center><br>
<center><input type=”text” name= 'email' value="<?PHP print $email;?>" size=40 maxlength=100></center>
<p><center><strong>Password</strong></center><br>
<center><input type=”text” name= 'password' value="<?PHP print $pword;?>" size=40 maxlength=20></center>

<P align = center>
<INPUT TYPE = "Submit" Name = "Submit1"  VALUE = "Login">
<form action="Register.php"> <input type="submit" value="Sign Up"> </form>
4

1 回答 1

2

首先,恭喜您开始编码。我希望你玩得开心!

看起来您可能会遇到“区分大小写”的情况。我注意到您在顶部有以下代码:

$email = $_POST['Email'];
$pword = $_POST['Password'];

但是,在您的 HTML 中,您实际上是在传递以全小写命名的那些变量。尝试将顶部的代码更改为:

$email = $_POST['email'];
$pword = $_POST['password'];

或者您在“电子邮件”和“密码”中输入的名称(同样,请注意首字母大写)。检查问题是否在这里(与查询中的某些内容相比)的一种简单方法是

var_dump($_POST);

看看你的脚本从表单提交中得到了什么。

有关更多信息,请参阅 PHP 的http://php.net/manual/en/language.variables.basics.php或查看相关帖子以了解如何进行自己的不区分大小写检查,但会被警告:这是更多的工作。PHP:不区分大小写的参数

于 2013-04-12T18:22:17.183 回答