您好我正在尝试创建一个登录系统,而我在后端的技能非常有限。我用了一个教程来创建一个数据库,意识到我的其他页面上的所有php都使用了pdo,这个教程是mysqli。
我尝试修改此代码以尝试调整它,但是我的尝试没有成功。
非常厚颜无耻但非常感谢,如果有人可以编辑代码以使用PDO:)。
非常感谢
<?php
try {
$handler = new PDO('mysql:host=127.0.0.1;dbname=loginsytem', 'root', '');
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
echo $e->getName();
die();
}
session_start();
$query = $handler->query('SELECT * FROM users');
if (isset($_POST['submit'])) {
incude_'dbh.inc.php';
$uid = PDO::quote($conn, $_POST['uid']);
$pwd = PDO::quote($conn, $_POST['pwd']);
//Error Handlers
//Check if inputs are empty
if (empty($uid)) || empty($pwd)) {
header("location: ../index.php?login=empty");
exit();
}
} else {
$sql = "SELECT * FROM users WHERE user_uid='$uid'";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck < 1) {
header("location: ../index.php?login=error");
exit();
} else {
if ($row = mysqli_fetch_assoc($result)) {
//de-hashing the password
$hashedPwdCheck = password_verify($pwd, $row['user_pdw']);
if ($hashedPwdCheck == false) {
header("location: ../index.php?login=error");
exit();
} elseif ($hashedPwdCheck == true) {
//Log in the user here
$_SESSION['u_id'] = $row['user_id'];
$_SESSION['u_uid'] = $row['user_uid'];
header("location: ../index.php?login=success");
exit();
}
}
}
}
else {
header("location: ../index.php?login=error");
exit();
}
?>
答案 0 :(得分:1)
这里你去..通过比较你的代码注意改变..要学习PDO与MySQL,请参阅本教程http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
<?php
try {
$handler = new PDO('mysql:host=127.0.0.1;dbname=loginsytem', 'root', '');
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
echo $e->getName();
die();
}
session_start();
//$query = $handler->query('SELECT * FROM users');
if (isset($_POST['submit'])) {
//Error Handlers
//Check if inputs are empty
if (empty($uid)) || empty($pwd)) {
header("location: ../index.php?login=empty");
exit();
}
} else {
$stmt = $db->prepare("SELECT * FROM users WHERE user_uid=:uid");
$stmt->bindParam(':uid', $uid, PDO::PARAM_STR);
if ($stmt->execute()) {
header("location: ../index.php?login=error");
exit();
} else {
if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
//de-hashing the password
$hashedPwdCheck = password_verify($pwd, $row['user_pdw']);
if ($hashedPwdCheck == false) {
header("location: ../index.php?login=error");
exit();
} elseif ($hashedPwdCheck == true) {
//Log in the user here
$_SESSION['u_id'] = $row['user_id'];
$_SESSION['u_uid'] = $row['user_uid'];
header("location: ../index.php?login=success");
exit();
}
}
}
}
else {
header("location: ../index.php?login=error");
exit();
}
?>