0

注意:未定义索引:第 14 行 C:\xampp\htdocs\login_in2.php 中的 myusername 用户名或密码错误

<?php

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php" 
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
4

4 回答 4

2

数据未提交,该错误与尝试访问 处的变量有关$_POST['']

一些简单的错误检查应该可以解决它:

<?php

[..]

if ( isset( $_POST['myusername'] ) && isset( $_POST['mypassword'] ) ) {

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

[...]

}
?>
于 2013-08-07T20:46:37.397 回答
0

这是您可以检查的 html 部分。您必须命名您的用户名输入“myusername”,因为您尝试使用

$myusername=$_POST['myusername'];

你必须在 html 代码上有这个

<input type="text" name="myusername" >
于 2013-08-07T20:51:48.120 回答
0

可能是你在混合你的引号吗?代替

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"

你可以试试

$sql="SELECT * FROM $tbl_name WHERE username='".$myusername."' and password='".$mypassword."'";
于 2013-08-07T20:45:44.243 回答
0

可能输入表单名称有错字。代替

 $myusername=$_POST['myusername']; with $myusername=$_POST['username'];

$mypassword=$_POST['mypassword']; with $mypassword=$_POST['password'];

在所有情况下。

于 2013-08-07T20:51:21.557 回答