amount" /-6ren">
gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:48:18 27 4
gpt4 key购买 nike

我已经用 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/

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