【问题标题】:Undefined index: username in xampp未定义的索引:xampp 中的用户名
【发布时间】:2015-04-09 11:21:05
【问题描述】:

我一直用和写的一样。但是每当我点击它时,它都会出错

注意:未定义索引:第 3 行 D:\xamp\htdocs\xampp\new\login.php 中的用户名
注意:未定义索引:第 4 行 D:\xamp\htdocs\xampp\new\login.php 中的密码
请输入用户名和密码

我的 HTML 是:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="login.php" method="POST">Username:
<input type ='text' name="username" /><br />
password<input type="password" name="password" /><br />
<input type="submit" value="login" /></form>
</body>
</html>

虽然我的 PHP 是:

<?php

$username = '$_POST[username]';
$password = '$_POST[password]';
if($username && $password)
{
$connect = mysql_connect(“localhost”, “root”, “”); 
$query=mysql_query("SELECT * FROM usres WHERE username=$username");
$numrows = mysql_num_rows($query);
if (!connect) { 
die('Connection Failed: ' . mysql_error()); 
}
   mysql_select_db(“phplogin”, $connect);

}else{
    die("please enter username and password");



    enter code here

}


?>

【问题讨论】:

  • $username = '$_POST[username]'; 删除引号,为另一个引号和 [username]' 添加引号 ['username']'。然后将大括号 “ ” 更改为 " 然后在 where 子句中引用此 $username;这是一个字符串。
  • 这有点令人沮丧,我仍然必须一直在标签中保持打开状态:stackoverflow.com/questions/12859942/… ...而且您很容易受到 SQL 注入攻击...而且您似乎有一些“花哨”的报价mysql_connect(“localhost”, “root”, “”);
  • 不进行任何错误报告/检查。您甚至不会首先连接到 DB,而不是使用您正在使用的引号,而是会产生 500 错误。
  • ['username']' => ['username'] 我的额外报价; 我的错

标签: php html mysql xampp


【解决方案1】:

试试这个

<?php

$username = $_POST['username'];
$password = $_POST['password'];
$connect = mysql_connect("localhost", "root", ""); 
if (!$connect) { 
    die('Connection Failed: ' . mysql_error()); 
}
mysql_select_db("phplogin", $connect);

if($username && $password) {
    $query=mysql_query("SELECT * FROM usres WHERE username='".$username."' ");
    $numrows = mysql_num_rows($query);
    if($numrows > 0){
        //redirect to other page
    }else{
        echo "please enter username and password";
    }
} else {
    echo "please enter username and password";
}

?>

【讨论】:

猜你喜欢
  • 2018-11-26
  • 1970-01-01
  • 2014-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-17
  • 1970-01-01
相关资源
最近更新 更多