我有以下代碼。現在,當我按下登錄按鈕時,什麼也沒有發生,並且用戶名和密碼被清除。PHP登錄問題
<?php
session_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="csduc"; // Database name
$tbl_name="students"; // Table name
// Connect to server and select databse.
$connect=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['username'];
$mypassword=$_POST['password'];
// To protect MySQL injection (more detail about MySQL injection).
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql,$connect);
$row=mysql_fetch_array($result);
// 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($row)
{
// Register $myusername, $mypassword and redirect to file "login_success.php".
session_register("myusername");
session_register("mypassword");
header("location: main.php");
}
else
{
echo "Wrong Username or Password";
}
?>
我該如何解決這個問題?
表單代碼在哪裏? – 2011-02-23 07:37:58
打印您的$ sql以查看存儲的內容。什麼都沒有回報?把你的HTML(只是在表單裏面的登錄位)看一看 – JohnP 2011-02-23 07:38:44
session_register已被棄用:http://php.net/manual/en/function.session-register.php – 2011-02-23 07:39:11