gpt4 book ai didi

php - 登录后如何访问特定的mysql数据行和列?

转载 作者:行者123 更新时间:2023-11-29 07:55:27 25 4
gpt4 key购买 nike

下面的脚本是我用来检查用户名和密码是否在我的数据库中的脚本。这很好,我现在想要的是保留在用户名和密码所在的行中。在不同列下的同一行中是有关用户的更多信息。我想在不同的 div 中显示其中一些信息。不是全部。除密码和电子邮件之外的列示例为“FirstName”和“LastName”

还有一个“Id”列,如果能够执行“您已登录,您的 ID 为 10101,然后显示当前 ID 中的名字和姓氏”之类的操作,那就太好了。

<?
session_start();
//then do rest of the stuffs//
?>

<?php


if (!isset($_POST['submit'])){
?>


<!-- The HTML login form -->
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Email: <input type="text" name="Email" /><br />
Password: <input type="password" name="Password" /><br />

<input type="submit" name="submit" value="Login" />
</form>
<?php


} else {

require_once("db_const.php");
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
# check connection
if ($mysqli->connect_errno) {
echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
exit();
}

$Email = $_POST['Email'];
$Password = $_POST['Password'];


$sql = "SELECT * from stores_db WHERE Email = '{$Email}' AND Password = '{$Password}' LIMIT 1";
$result = $mysqli->query($sql);
if (!$result->num_rows == 1)
{
echo "<p>Invalid Email or Password combination</p>";
}
else
{

$recordset = mysqli_fetch_array($result);

$temp_firstname=$recordset['FirstName'];


$temp_lasttname=$recordset['LastName'];

$temp_id=$recordset['Id'];

$_SESSION['user_id']=$temp_id;

header("location:homepage.php"); // direct to your page to show

}

}
?>

homepage.php 上的代码

 <?php

session_start(); echo $_SESSION['user_id']; //to display the id of the logged in user

?>

最佳答案

$sql = "SELECT * from stores_db WHERE Email LIKE '{$Email}' AND Password LIKE '{$Password}'   LIMIT 1";
$result = $mysqli->query($sql);
if (!$result->num_rows == 1)
{
echo "<p>Invalid username/password combination</p>";
}
else
{
echo "<p>Logged in successfully</p>";


$recordset = mysqli_fetch_array($result);

$temp_firstname=$recordset['FirstName'];
// storing firstname on a variable

$temp_lasttname=$recordset['LastName'];

$temp_id=$recordset['id'];

$_SESSION['user_id']=$temp_id;

header("location:homepage.php"); // direct to your page to show


}

完成此操作后,

session_start(); echo $_SESSION['user_id'];//显示登录用户的id

在被愚弄的页面

关于php - 登录后如何访问特定的mysql数据行和列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25260740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com