首页 > 解决方案 > 如何将一行连接到数据库中的表?

问题描述

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

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

有没有办法解决这个问题?

<?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 表单分开

标签: phpmysqlmysqli

解决方案


您可以将他的用户 ID 存储在会话中

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

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

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

如果他点击注销

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

推荐阅读