php登录表单验证问题

时间:2013-12-01 09:03:29

标签: php mysql forms validation login

我编写了一个脚本,允许用户登录我的网站,直到最近,我认为它工作正常。但是,它似乎没有正确检测数据库字段。我不确定问题是否与我的验证或我的实际登录表单有关。

当我尝试登录时,我收到此错误 -

  

警告:mysql_fetch_array()期望参数1是资源,   给出的布尔值   /家庭/ * / O * ** * * < /strong>*n.co.uk/tut/Login/validate_login.php on   第22行

我在第22行添加了评论。

这是login.php

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>1D Affection | Login</title>
    </head>
    <body>
        <form method="post" action="validate_login.php" >
            <table border="1" >
                <tr>
                    <td><label for="uname">Username</label></td>
                    <td><input type="text" 
                      name="uname" id="uname"></td>
                </tr>
                <tr>
                    <td><label for="pass">Password</label></td>
                    <td><input name="pass" type="password" id="pass"></td>
                </tr>
                <tr>
                    <td><input type="submit" value="Submit"/>
                    <td><input type="reset" value="Reset"/>
                </tr>
            </table>
        </form>
    </body>
    </html>

And this is the validation code -

<?php
session_start();
  $_SESSION['uname'] = $uname;

// Grab User submitted information
$uname = $_POST["uname"];
$pass = $_POST["pass"];

// Connect to the database
$con = mysql_connect("mysql.****************.co.uk","robjef2","************");
// Make sure we connected succesfully
if(! $con)
{
    die('Connection Failed'.mysql_error());
}

// Select the database to use
mysql_select_db("onedirectionaffection_members",$con);

$result = mysql_query("SELECT uname, pass FROM users WHERE uname = $uname");

$row = mysql_fetch_array($result);  ///this is line 22 that the error describes.

if($row["uname"]==$uname && $row["pass"]==$pass)
  header("Location: ../../profile/profile.php");
else
    echo "Sorry, your credentials could not be found on our system. Please try again.";

$_SESSION['uname'] = $uname;

?>

我将指出原来这个脚本需要登录的电子邮件地址,但我将其更改为用户名,或者就mysql而言,uname。

我是这方面的初学者但是你能给我的任何建议我会尽我所能来解决它​​。如果您需要有关此问题的任何进一步详细信息,请与我们联系。

非常感谢提前。

2 个答案:

答案 0 :(得分:1)

您的查询无法执行。其主要原因似乎是你没有将字符串值括在引号

SELECT uname, pass FROM users WHERE uname = $uname

应该是

SELECT uname, pass FROM users WHERE uname = '$uname'

如果您使用MySQLiPDO移至准备好的语句,则无需担心这些事情或SQL注入。

答案 1 :(得分:0)

你在分配到会话时,$ uname是emtyp,以这种方式尝试脚本,

 $_SESSION['uname'] = $_POST["uname"];

// Grab User submitted information in this way
if(isset($_POST["uname"]) && isset($_POST["pass"]))
{
    $uname = $_POST["uname"];
    $pass = $_POST["pass"];
}

查询可以这种方式运行,

$recordFound = mysql_num_rows($result);
if($recordFound > 0)
{
     $row = mysql_fetch_array($result);
     // do stuff here ...
}
else
     echo "Sorry, your credentials could not be found on our system. Please try again.";