PHP登录表单问题与编码

时间:2013-08-07 20:42:38

标签: php

  

注意:未定义的索引:第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 个答案:

答案 0 :(得分:2)

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

一些简单的错误检查应该修复它:

<?php

[..]

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

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

[...]

}
?>

答案 1 :(得分:0)

你能混合你的引号吗?而不是

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

您可以尝试

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

答案 2 :(得分:0)

可能输入的表单名称有拼写错误。 替换

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

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

在所有情况下。

答案 3 :(得分:0)

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

进行访问
$myusername=$_POST['myusername'];

你必须在html代码

上有这个
<input type="text" name="myusername" >