[英]PHP login form issue with coding
注意:未定義的索引:第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";
}
?>
數據未提交,錯誤與嘗試訪問$_POST['']
的變量有關。
一些簡單的錯誤檢查應該修復它:
<?php
[..]
if ( isset( $_POST['myusername'] ) && isset( $_POST['mypassword'] ) ) {
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
[...]
}
?>
難道你是在混合你的引號嗎? 代替
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"
你可以試試
$sql="SELECT * FROM $tbl_name WHERE username='".$myusername."' and password='".$mypassword."'";
可能輸入表單名稱有拼寫錯誤。 更換
$myusername=$_POST['myusername']; with $myusername=$_POST['username'];
和
$mypassword=$_POST['mypassword']; with $mypassword=$_POST['password'];
在所有情況下。
這是您可以檢查的HTML部分。 您必須將用戶名輸入命名為“myusername”,因為您嘗試使用它來訪問它
$myusername=$_POST['myusername'];
你必須在html代碼上有這個
<input type="text" name="myusername" >
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.