我正在尝试从mysql_connect(完美地工作)更改我的登录页面以使用PDO,但没有运气。
每当我使用正确的用户名和密码点击“登录”时,它只刷新相同的登录页面。
提前致谢!
使用mysql_connect工作代码:
<?
session_start();
$user = "XXXX";
$password = "YYYY";
?>
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<br><br><br><br><br><br><br><br>
<div align="center"><h1>Home</h1>
<h3>
<? if (isset($_SESSION["authenticated"])) { ?>
You are logged in!
<br />
<a href="logout.php">log out</a>
<a href="userpage.php">See page</a>
<? } else { ?>
You are not logged in!
<? } ?>
</h3>
<br>
<?
if (($connection = mysql_connect("localhost", $user, $password)) === false)
die("Could not connect to database");
// select database
if (mysql_select_db("123456", $connection) === false)
die("Could not select database");
// if username and password were submitted, check them
if (isset($_POST["name"]) && isset($_POST["password"]))
{
// prepare SQL
$sql = sprintf("SELECT * FROM students WHERE name='%s'",
mysql_real_escape_string($_POST["name"]));
// execute query
$result = mysql_query($sql);
if ($result === false)
die("Could not query database");
// check whether we found a row
if (mysql_num_rows($result) == 1)
{
// fetch row
$row = mysql_fetch_assoc($result);
// check password
if ($row["password"] == $_POST["password"])
{
// remember that user's logged in
$_SESSION["authenticated"] = true;
$host = $_SERVER["HTTP_HOST"];
$path = rtrim(dirname($_SERVER["PHP_SELF"]), "/\\");
header("Location: http://$host$path/userpage.php");
exit;
}
}
}
?>
<form action="<?= $_SERVER["PHP_SELF"] ?>" method="post">
<table>
<tr>
<td>Username:</td>
<td>
<input name="name" type="text"></td>
</tr>
<tr>
<td>Password:</td>
<td><input name="password" type="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Log In"></td>
</tr>
</table>
</form></div>
</body>
</html>
使用PDO的新代码(不起作用):
<?
session_start();
$user = "XXXX";
$password = "YYYY";
$dbh = new PDO('mysql:host=localhost;dbname=123456', $user, $password);
?>
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<br><br><br><br><br><br><br><br>
<div align="center"><h1>Home</h1>
<h3>
<? if (isset($_SESSION["authenticated"])) { ?>
You are logged in!
<br />
<a href="logout.php">log out</a>
<a href="userpage.php">See page</a>
<? } else { ?>
You are not logged in!
<? } ?>
</h3>
<br>
<?
// if username and password were submitted, check them
if (isset($_POST["name"]) && isset($_POST["password"]))
{
// prepare SQL
$idd = $_POST["name"];
$qry = "SELECT * FROM students WHERE name='$idd'";
$result = $dbh->query($qry);
if ($result === false)
die("Could not query database");
if (mysql_num_rows($result) === false)
die("No luck!");
if (mysql_num_rows($result) == 1)
{
// fetch row
$row = mysql_fetch_assoc($result);
// check password
if ($row["password"] == ($_POST["password"]))
{
// remember that user's logged in
$_SESSION["authenticated"] = true;
$host = $_SERVER["HTTP_HOST"];
$path = rtrim(dirname($_SERVER["PHP_SELF"]), "/\\");
header("Location: http://$host$path/userpage.php");
exit;
}
}
}
?>
<form action="<?= $_SERVER["PHP_SELF"] ?>" method="post">
<table>
<tr>
<td>Username:</td>
<td>
<input name="name" type="text"></td>
</tr>
<tr>
<td>Password:</td>
<td><input name="password" type="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Log In"></td>
</tr>
</table>
</form></div>
</body>
</html>
答案 0 :(得分:0)
您正在查询方法返回的PDOStatement对象上使用mysql_
*函数。要获取行数,请尝试$result->rowCount()
。要获取记录,请使用其中一种获取方法。见link