未定义的索引$ myusername和$ mypassword?

时间:2014-01-01 21:18:40

标签: php mysql login mysqli

我一直在努力创建一个登录页面,只需登录并注销即可。我似乎总是出错。这一次,我使用了一个脚本,我在第15行和第16行收到错误,它一直说“注意:未定义的索引:第15行的C:\ xampp \ htdocs \ phpstuff \ main_login.php中的$ myusername < / p>

注意:未定义的索引:第16行的C:\ xampp \ htdocs \ phpstuff \ main_login.php中的$ mypassword 错误的用户名或密码“当我甚至没有输入我的密码或用户名时。我是新手,所以如果你能帮助我,那就太棒了。这就是我的所有代码。

main_login.php:

<?php

ob_start();
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="lordhelpme"; // 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");

 // Define $myusername and $mypassword 
 $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"
  session_register("myusername");
  session_register("mypassword"); 
  header("location:login_success.php");
  }
  else {
  echo "Wrong Username or Password";
  }
  ob_end_flush();
  ?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1"     bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr> 
</table>
</td>
</form>
</tr>
</table>
</body>
</html>

4 个答案:

答案 0 :(得分:1)

删除$符号:

$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

http://www.php.net/manual/en/reserved.variables.post.php

此外,您不需要引号:

mysql_connect($host, $username, $password)or die("cannot connect"); 
mysql_select_db($db_name)or die("cannot select DB");

您使用的是变量,而不是文字。

答案 1 :(得分:0)

更改

$myusername=$_POST['$myusername']; 
$mypassword=$_POST['$mypassword']; 

to 

$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

答案 2 :(得分:0)

除了更改POST值 -

$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

你需要将你的php代码放在if()中,这样只有在你的登录表单被提交时才会执行,$_POST['myusername']&amp;初始页面加载时未设置$_POST['mypassword'] -

<?php

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

ob_start();

... // truncated rest of your code

 // Define $myusername and $mypassword 
 $myusername=$_POST['myusername']; 
 $mypassword=$_POST['mypassword']; 



... // truncated rest of your code


  ob_end_flush();

 }
 ?>

在这种情况下,使用isset()来测试您的表单发布值是否已设置。

修改

另外,将表单操作更改为""以使表单发布到自身 - &gt; <form name="form1" method="post" action="">

答案 3 :(得分:0)

这张表格不是张贴在自己身上吗?然后

<form name="form1" method="post" action="checklogin.php">

应该是

<form name="form1" method="post" action="main_login.php">
or
<form name="form1" method="post" action="<?php echo(htmlentities($_SERVER['PHP_SELF'])); ?>">

但是当页面启动时,将你的php代码放在if语句中:

if($_POST)
{...}

e.g。

if($_POST)
{
ob_start();
$host="localhost";.............
}

此时页面尚未从表单中发布。换句话说,在这个时间点,$ _POST ['myusername']是未定义的,它不存在。发布以下内容:

<?php

if($_POST)
{
    ob_start();
    $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password=""; // Mysql password 
    $db_name="lordhelpme"; // 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");

     // Define $myusername and $mypassword 
     $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"
      session_register("myusername");
      session_register("mypassword"); 
      header("location:login_success.php");
      }
      else {
      echo "Wrong Username or Password";
      }
      ob_end_flush();
      ?>

}

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1"     bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="main_login.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr> 
</table>
</td>
</form>
</tr>
</table>
</body>
</html>