加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP中未定义的变量错误

发布时间:2020-12-13 22:21:47 所属栏目:PHP教程 来源:网络整理
导读:Notice: Undefined variable: username in C:xampphtdocstest_class.php on line 20Notice: Undefined variable: password in C:xampphtdocstest_class.php on line 20 当我使用这段代码检查我的数据库的用户名和密码时,我收到上述错误. ?php class te
Notice: Undefined variable: username in C:xampphtdocstest_class.php
        on line 20
Notice: Undefined variable: password in C:xampphtdocstest_class.php
        on line 20

当我使用这段代码检查我的数据库的用户名和密码时,我收到上述错误.

<?php
    class test_class {

        public function __construct() { 

        }
        public function doLogin() {

            include("connection.php");

            if (isset($_POST['username']))
                {
                $username= $_POST['username'];
                }
                if (isset($_POST['password']))
                {
                $password= $_POST['password'];
                } 

            $query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
            $result = mysql_fetch_array(mysql_query($query));
            if(!$result)

            {

            return 'assa';

            }else{

            return 'assa112121212';

            }

                }
        }
?>

解决方法

这很可能意味着您的表单尚未提交.您应该确保只使用变量(如果存在).此外,您应该使用来自用户的输入而不验证它.请尝试以下操作,例如:

if (isset($_POST['username']) && isset($_POST['password']))
{
    $username= $_POST['username'];
    $password= $_POST['password'];
    $query = "SELECT *
                      FROM users
                      WHERE username = '".mysql_real_escape_string($username)."'
                      AND password = '".mysql_real_escape_string($password)."'";
    $result = mysql_fetch_array(mysql_query($query));
    # ...
}
else
{
    return NULL;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读