我创建了下面的代码供人们登录网站,但结果一直显示:
错误的用户名或密码
我不知道出了什么问题。数据库有一个表“clients”,列名为“usernames”和“passwords”。
<?php
$host = ""; // Host name
$username = ""; // Mysql username
$password = ""; // Mysql password
$db_name = ""; // Database name
$tbl_name = "clients"; // Table name
$con = mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("$db_name") or die("cannot select DB");
$username = $_POST['myusername'];
$password = $_POST['mypassword'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql = "SELECT `username` FROM `clients` WHERE `username`='$myusername' and `password`='$mypassword'";
$result = mysql_query($sql) or die(mysql_error());
// Mysql_num_row is counting table row
if ($result) {
$count = mysql_num_rows($result);
}
else {
$count = 0;
}
// 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:source/login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>