php - 从数据库中检索值并将其存储为 session 变量

标签 php html css sql

我已经用 html 创建了一个付款表单。表单中的值是使用 session 自动添加的。当用户登录时,使用用户信息添加值。我的 html 如下:

<table>
    <form name="postForm" action="form_process.php" method="POST" >
    <tr><td><input type="hidden" name="txnid" value="<?php echo $txnid=time().rand(1000,99999); ?>" /></td></tr>
    <tr><td>amount</td><td><input type="text" name="amount" value="<?php echo $_SESSION['amount']; ?>" /></td></tr>
    <tr><td>firstname</td><td><input type="text" name="firstname" value="<?php echo $_SESSION['firstname']; ?>" /></td></tr>
    <tr><td>email</td><td><input type="text" name="email" value="<?php echo $_SESSION['email']; ?>" /></td></tr>
    <tr><td>phone</td><td><input type="text" name="phone" value="<?php echo $_SESSION['phone']; ?>" /></td></tr>
    <tr><td>productinfo</td><td><input type="text" name="productinfo" value="<?php echo $_SESSION['productinfo']; ?>" /></td></tr>
    <tr><td><input type="hidden" name="surl" value="success.php" size="64" /></td></tr>
    <tr><td><input type="hidden" name="furl" value="fail.php" size="64" /></td></tr>
    <tr><td><input type="submit" /></td><td><input type="reset" /></td></tr>
    </form>
</table>

在登录页面中,我将 session 存储如下:

<?php
session_start();
include("config.php");

if(isset($_POST['login']))
{
$user_email=$_POST['email'];
$user_pass=$_POST['pass'];

$check_user="select * from users WHERE user_email='$user_email'AND user_pass='$user_pass'";

$run=mysqli_query($dbcon,$check_user);

if(mysqli_num_rows($run)>0)
{
$result=mysqli_fetch_array($run);


$_SESSION['email']=$user_email;

$_SESSION['access']=$result['access'];


//here session is used and value of $user_email store in $_SESSION.
echo "<script>window.open('index.php','_self')</script>";

}
else
{
echo "<script>alert('Email or password is incorrect!')</script>";
}
}

?>
<html>
<head lang="en">
<meta charset="UTF-8">
<link type="text/css" rel="stylesheet" href="bootstrap-3.2.0-dist\css\bootstrap.css">
<title>Login</title>
<link rel="icon" type="image/png" href="images/imageedit_2_5125240109.gif"/>
<link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="all" />
<link href="css/style.css" rel="stylesheet" type="text/css" media="all" />
<link href="css/appointment_style.css" rel="stylesheet" type="text/css" media="all" />
<link href="css/font-awesome.css" rel="stylesheet">
<!-- //for bootstrap working -->
<link href="//fonts.googleapis.com/css?family=Source+Sans+Pro:300,300i,400,400i,600,600i,700" rel="stylesheet">

</head>
<style>
.login-panel {
margin-top: 150px;
}
</style>

<body>


<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="login-panel panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">Sign In</h3>
</div>
<div class="panel-body">
<form role="form" method="post" action="login.php">
<fieldset>
<div class="form-group" >
<input class="form-control" placeholder="E-mail" name="email" type="email" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="pass" type="password" value="">
</div>


<input type="submit" value="login" name="login" >

<!-- Change this to a button or input when using this as a form -->
<!-- <a href="index.html" class="btn btn-lg btn-success btn-block">Login</a> -->
</fieldset>
</form>
</div>
</div>
</div>
</div>
</div>


</body>

</html>

用户邮箱在表单中输入正确,但手机号码和其他详细信息未添加到表单中。我试过像上面那样将它们串成变量,但它没有显示。我想我需要从数据库中获取详细信息。谁能告诉我该怎么做?

最佳答案

我做了很多研究,终于自己找到了答案。

我使用 $_SESSION 和 select 查询从数据库中获取当前用户的信息。并使用 while 循环将它们放入变量中。 更新后的代码如下:

<div  class="sing"><?php
session_start();

if (isset($_SESSION['email']) && $_SESSION['email'] == true) {
  echo " &nbsp Welcome  " . $_SESSION['email'] ;
echo "<div style='float: right;'><a href='logout.php'>Logout</a>&nbsp</div>";
} else {

}


?>
</div>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
	<div>
		<h2>Payment Gateway Testing Sample</h2>
		<h3>Fill the form and submit it for starting the transaction...</h3>
	</div>

<div>



  <?php

  include("config.php");


$user_email=$_SESSION['email'];
  $check_user="select * from users WHERE user_email='$user_email'";

  $run=mysqli_query($dbcon,$check_user);

while($row = $run->fetch_assoc())
{

 $user_name=$row['user_name'];
 $user_mobile=$row['user_mobile'];

}
  //here session is used and value of $user_email store in $_SESSION.




  ?>


<table>
	<form name="postForm" action="form_process.php" method="POST" >
	<tr><td><input type="hidden" name="txnid" value="<?php echo $txnid=time().rand(1000,99999); ?>" /></td></tr>
	<tr><td>amount</td><td><input type="text" name="amount" value="" /></td></tr>



	<tr><td>Name</td><td><input type="text" name="firstname" value="<?php echo $user_name; ?>" /></td></tr>
	<tr><td>email</td><td><input type="text" name="email" value="<?php echo $_SESSION['email']; ?>" /></td></tr>
	<tr><td>phone</td><td><input type="text" name="mobile" value="<?php echo $user_mobile; ?>" /></td></tr>
	<tr><td>productinfo</td><td><input type="text" name="productinfo" value="" /></td></tr>
	<tr><td><input type="hidden" name="surl" value="success.php" size="64" /></td></tr>
	<tr><td><input type="hidden" name="furl" value="fail.php" size="64" /></td></tr>
	<tr><td><input type="submit" /></td><td><input type="reset" /></td></tr>
	</form>
</table>
</div>
</body>
</html>

关于php - 从数据库中检索值并将其存储为 session 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50674797/

相关文章:

php - mysql 中的事件调度程序每 1 分钟运行一次

html - Bootstrap 3 : Push/pull columns only on smaller screen sizes

html - 如何让图像和其他元素留在原地

CSS继承填充,然后减少数量

php - Magento 集合过滤功能

php headers_sent 函数不工作

php - 在 PHP 中实例化对象后立即使用对象运算符

html - 如何从 ASP.NET AJAX 控件工具包中的 HTML 编辑器获取纯文本?

html - 隐藏 Kendo DatePicker 按钮 - 有选择地(CSS)

css - 无法将模板背景颜色更改为透明