从数据库中检索值并将其存储为会话变量

时间:2018-06-04 06:49:49

标签: php html css sql

我已在html中创建了一个付款表单。表单中的值会自动添加会话。当用户登录时,使用用户信息添加值。我的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>
&#13;
&#13;
&#13;

在登录页面中,我已按如下方式存储会话:

&#13;
&#13;
<?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>
&#13;
&#13;
&#13;

用户电子邮件在表单中正确输入,但手机号码和其他详细信息未添加到表单中。我尝试将它们串起来作为上面的变量,但它没有显示。我想我需要从数据库中获取详细信息。谁能告诉我怎么做?

1 个答案:

答案 0 :(得分:0)

我做了很多研究,最后自己回答了问题。

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

&#13;
&#13;
<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>
&#13;
&#13;
&#13;