php $_POST 不起作用。“注意:未定义索引:用户名...”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14115904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:40:58  来源:igfitidea点击:

$_POST not working. "Notice: Undefined index: username..."

phphttp-postundefined-index

提问by blacblu

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

可能重复:
PHP:“注意:未定义变量”和“注意:未定义索引”

So, I am currently learning PHP and was reading on a book about the md5 function for passwords, so I decided to give it a try and see how it goes. I also decided to use the POST method rather than the GET, since I saw people saying that it is safer and doesn't let the variables appearing on the URL.

所以,我目前正在学习 PHP 并且正在阅读一本关于密码的 md5 函数的书,所以我决定尝试一下,看看它是如何进行的。我还决定使用 POST 方法而不是 GET,因为我看到人们说它更安全并且不会让变量出现在 URL 上。

For my testing project I made a very simple form, which follows:

对于我的测试项目,我制作了一个非常简单的表格,如下所示:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <form action="dologin" method="POST">
            <table border="0">
                <tbody>
                    <tr>
                        <td>Username:</td>
                        <td><input type="text" name="username"></td>
                    </tr>
                    <tr>
                        <td>Password:</td>
                        <td><input type="password" name="password"></td>
                    </tr>
                </tbody>
            </table>
            <input type="submit" value="Login">
        </form>
    </body>
</html>

The problem is that when I enter the values on BOTH fields and click "Login" I get the following output on the other PHP file.

问题是,当我在两个字段中输入值并单击“登录”时,我在另一个 PHP 文件中得到以下输出。

Notice: Undefined index: username in C:\xampp\htdocs\md5\home\dologin\index.php on line 11
Username non existent

Here follows the code for the "/dologin/index.php" file

下面是“/dologin/index.php”文件的代码

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
            mysql_connect("localhost", "root") or die(mysql_error());
            mysql_select_db("test") or die(mysql_error());
            $query = "SELECT password FROM users WHERE username='".$_POST['username']."'";
            $result = mysql_query($query);
            $row = mysql_num_rows($result);
            if(isset($row)){
                $password = (mysql_result($result,$row - 1));
                $enteredpassword = md5("w@#$@#".$_GET['password']."^#3f%#^");
                if($password == $enteredpassword){
                    if(!isset($_COOKIE['PHPSESSID'])){
                        session_start();
                    }
                    $_SERVER['LOGIN_STATUS'] = true;
                } else {
                    die("Access denied");
                }    
            } else {
                die("Username non existent");
            }

        ?>
    </body>
</html>

Any help at all on my issue is very much appreciated, thank you for reading.

非常感谢您对我的问题的任何帮助,感谢您的阅读。

回答by Josh Brody

undefined indexmeans that somewhere in the $_POST array, there isn't an index (key) for the key username.

未定义索引意味着 $_POST 数组中的某处,没有键username的索引(键)。

You should be setting your posted values into variables for a more clean solution, and it's a good habit to get into.

您应该将发布的值设置为变量以获得更干净的解决方案,这是一个好习惯。

If I was having a similar error, I'd do something like this:

如果我有类似的错误,我会做这样的事情:

$username = $_POST['username']; // you should really do some more logic to see if it's set first
echo $username;

If username didn't turn up, that'd mean I was screwing up somewhere. You can also,

如果用户名没有出现,那意味着我在某个地方搞砸了。你也可以,

var_dump($_POST);

To see what you're posting. var_dump is really useful as far as debugging. Check it out: var_dump

要查看您发布的内容。var_dump 在调试方面非常有用。检查一下:var_dump

回答by marvin

first of all,

首先,

be sure that there is a post

确定有帖子

if(isset($_POST['username'])) { 
    // check if the username has been set
}

second, and most importantly, sanitize the data, meaning that

其次,也是最重要的,清理数据,这意味着

$query = "SELECT password FROM users WHERE username='".$_POST['username']."'";

is deadly dangerous, instead use

是致命的危险,而是使用

$query = "SELECT password FROM users WHERE username='".mysql_real_escape_string($_POST['username'])."'";

and please research the subject sql injection

并请研究主题sql 注入

回答by silent_coder14

You should check if the POST['username']is defined. Use this above:

您应该检查是否POST['username']已定义。使用上面这个:

$username = "";

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

"SELECT password FROM users WHERE username='".$username."'"