php - 如何将一行连接到数据库中的表?

标签 php mysql mysqli

我目前正在为我们学校开发一个系统,我有一个注册/登录/考勤系统。

问题是我想让一个在我的表的一行中的用户连接到另一个表。只有他才能访问该表,因为似乎每当我们登录另一个用户时,它都会重定向到同一网页。

有什么办法可以解决这个问题吗?

<?php
$mymail = $_POST["mymail"];
$mypass = $_POST["mypass"];

echo "$mymail";

//database connection to check inside table and query email and password
$servername = "localhost";
$username = "root";
$password = "admin";
$dbname = "sistema";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM userlogin where mymail ='" . $mymail . "' and mypass = '" . $mypass . "'";
$result = $conn->query($sql);

// echo $sql;

if ($result->num_rows > 0) {
    // output data of each row
    // while($row = $result->fetch_assoc()) {
    //     echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["idnumber"]. "<br>";
    // }

header("Location: http://localhost/sistema/attendance/index.php"); /* Redirect browser */

} else {
header("Location: http://localhost/sistema/signup/index2.html"); /* Redirect browser */
    // echo "0 results";
}
$conn->close();
?>

//这只是与数据库的连接以及查询,我分离了 HTML 表单

最佳答案

您可以将他的用户 ID 存储在 session

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
//after login that user
$_SESSION["id"] = $_Post['id'];
$_SESSION["id"] = $_Post['username'];
?>

您必须在每个页面上启动 session ,并且无论如何您都可以在您的网站上使用该用户 ID 调用 $_SESSION["id"]

如果他点击注销

unset($_SESSION["id"]); & unset($_SESSION["id"]);

关于php - 如何将一行连接到数据库中的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54779797/

相关文章:

php - 安装 MySQLi 显示它将禁用 MySQL,这会影响我当前的代码吗?

php - 404 页面中的 Laravel 本地化

php - 一个实体上有多个 @UniqueEntity 注释

php - zend框架2重新连接到数据库

mysql - 在 SELECT * 查询中格式化日期

php - HTML 表单未将值传递给 PHP (mysqli_real_escape_string)

php - WP get_terms - 两种分类法,一种具有指定术语

mysql - 如何使用nodemon在Node.js上自动创建mysql数据库?

php - 创建自定义类结果集

php - 使用 PHP 获取数据库备份