php - undefined index $myusername 和 $mypassword?

标签 php mysql login mysqli

我一直在尝试创建一个登录页面,我只需在其中登录和注销。我似乎总是出错。这一次,我使用了一个脚本,在第 15 行和第 16 行出现错误,并且一直显示“注意:未定义索引:C:\xampp\htdocs\phpstuff\main_login.php 中的 $myusername 在第 15 行

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

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>

最佳答案

删除 $ 符号:

$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");

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

关于php - undefined index $myusername 和 $mypassword?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20873377/

相关文章:

php - mysql奇怪的多重内连接情况

php - DELETE 语句循环中的 bindValue

mysql - "No Return Found"简单的mysql函数错误

asp.net - 从 ASP.Net 登录控件 LoggedIn 事件获取 UserID

asp.net - 如何使用 Active Directory 使用 ASP.NET 4.0 构建基本登录页面?

javascript - 计算数组中的单词而不是字符

javascript - 我需要服务器时间,而不是本地时间 (JavaScript)

mysql - MySQL Workbench 中是否有用于应用(编辑模式)按钮的快捷方式(键盘)?

mysql - #1064 - 您的 SQL 语法有误;

wordpress - 在我的网站和 wordpress 博客之间共享 session