[英]how to check the data in the database?
Tell me how to configure authorization.告诉我如何配置授权。 Php I know basically, since I don't work with it, but I need to implement this task.
Php 我基本上知道,因为我不使用它,但我需要实现这个任务。 I would like that when clicking on the button authorization passes and goes to another page.
我希望当点击按钮授权通过并转到另一个页面时。 I do not fully understand how to check the data in the database and compare it with user data and, if there is a coincidence, re-direct to another page.
我不完全了解如何检查数据库中的数据并将其与用户数据进行比较,如果有巧合,请重定向到另一个页面。 Did you go the right way?
您的 go 方法是否正确? How can I solve my problem?
我该如何解决我的问题?
authdb.php authdb.php
<?php
$dbname = "dictdb";
$username = "root";
$userpass = "";
$charset = "utf8";
$options = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
$userdata = [
"logVal" => trim($_POST["logVal"]),
"passVal" => trim($_POST["passVal"])
];
$db = new PDO("mysql:host=$dbhost;dbname = $dbname;charset=$charset", $username, $userpass, $options);
if (!empty(trim($_POST["logVal"])) && !empty(trim($_POST["passVal"]))) {
$query = "SELECT * FROM `dictdb`.`user` WHERE `login` = :logVal AND `password` = :passVal";
$user_data = $query-fetchAll();
if ($user_data[password] == :passVal) {
echo "ok";
};
?>
auth.php auth.php
$(document).ready(function () {
$("button.btn-auth").on("click", function () {
$.ajax({
url: '/src/php/auth.php',
type: "POST",
});
});
});
First things first, you are doing a table search that will return only 1 row, so change your fetchAll to fetch -首先,您正在执行仅返回 1 行的表搜索,因此将 fetchAll 更改为 fetch -
// change this - $user_data = $query-fetchAll(); to
$user_data = $query-fetch(PDO::FETCH_ASSOC);
//This part is also incorrect - if ($user_data[password] == :passVal) as there is no $user_data[password] and will return an error... Change to -
if ($user_data[passVall] == :passVal)
You are then posting to the wrong page, your PHP login data will never run - //url: '/src/php/auth.php' should be - url: '/src/php/authdb.php',然后您发布到错误的页面,您的 PHP 登录数据将永远不会运行 - //url: '/src/php/auth.php' 应该是 - url: '/src/php/authdb.php',
In your authdb.php page, return the $user_data to your auth.php page if login was successful -在您的 authdb.php 页面中,如果登录成功,则将 $user_data 返回到您的 auth.php 页面 -
if ($user_data[password] == :passVal) {
return $user_data;
//direct user back to your auth page -
include( '../auth.php' ); //check correct path...
};
Now do with the returned data as you wish, I would personally have loaded the returned data in my authdb.php page to a session though.现在按照您的意愿处理返回的数据,我个人会将我的 authdb.php 页面中的返回数据加载到 session 中。
Your post part is incorrect, you can do something to this effect -您的帖子部分不正确,您可以为此做一些事情-
$.post("../authdb.php", {
logVal: logVal,
passVal: passVal
},
function (data) {
if (data == '0') {
alert("Incorrect or Invalid Password entered.");
return false;
} else {
alert("Sign In successful.");
}
In your authdb.php file, after checking the passwords etc match -在您的 authdb.php 文件中,检查密码等匹配后 -
if ($user_data[passVal] == :passVal) {
echo count($user_data); //Will return 0 if failed, 1 if success
die;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.