-4

我对 php 很陌生,这是我第一次登录。所以当我从数据库中输入一个有效的用户名和密码时,我得到了这两个错误:

注意:未定义的索引:第 3 行的 brukernavn

注意:未定义的索引:第 4 行的 passord

$username = $_POST['brukernavn'];
$password = $_POST['passord'];

if($username&&$password)
{

$connect = mysql_connect("localhost", "root", "") or die ("Cannot connect");
mysql_select_db("prosjekt") or die ("Cant find db");

$query = mysql_query("SELECT * FROM bruker WHERE brukernavn='$username'");

$radnum = mysql_num_rows($query);

if ($radnum!=0)
{

while ($rad = mysql_fetch_assoc($query))
{
    $dbuser = $rad['brukernavn'];
    $dbpass = $rad['passord'];
}

if ($username==$dbuser&&$password==$dbpass)
{
echo "Logged in";
}
else
    echo "Wrong username or password";

}
else
    die("That user does not exist!");


}
else
    die("Please enter a username and password!");




?>

谁能看到我在这里出错的地方?

4

2 回答 2

2

您正在尝试访问两个尚未分配的变量:

$username = $_POST['brukernavn'];
$password = $_POST['passord'];

你要么:

  1. 需要将您的登录代码包装在if语句中以检查表单是否已提交

  2. 需要在访问它们的值之前检查是否设置了这些变量

于 2013-04-13T20:34:20.840 回答
0

希望这可以帮助...

 <?

$username = $_POST['brukernavn']; 
$password = $_POST['passord'];

if (isset ($_POST['submit'])) { // on form submit... make sure the html form input submit is name='submit' then this script will work

    if($username&&$password)
        {

        $connect = mysql_connect("localhost", "root", "") or die ("Cannot connect");
        mysql_select_db("prosjekt") or die ("Cant find db");

        $query = mysql_query("SELECT * FROM bruker WHERE brukernavn='$username'");

        $radnum = mysql_num_rows($query);

            if ($radnum!=0)
                {

                while ($rad = mysql_fetch_assoc($query))
                {
                    $dbuser = $rad['brukernavn'];
                    $dbpass = $rad['passord'];
                }

                if ($username==$dbuser&&$password==$dbpass)
                {
                echo "Logged in";
                }
                else
                    echo "Wrong username or password";

                }
            else
                die("That user does not exist!");


        }
    else
        die("Please enter a username and password!");

    }


?>
于 2013-04-13T20:53:34.830 回答