从数据库中检索值并将其存储为会话变量
Posted
技术标签:
【中文标题】从数据库中检索值并将其存储为会话变量【英文标题】:retreive values from database and store it as session variable 【发布时间】:2018-11-13 10:44:33 【问题描述】:我创建了一个 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>
在登录页面中,我将会话存储如下:
<?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>
用户邮箱在表单中输入正确,但手机号码等详细信息未添加到表单中。我尝试将它们作为上面的变量串起来,但它没有显示。我想我需要从数据库中获取详细信息。谁能告诉我该怎么做?
【问题讨论】:
您检查过$result
包含的内容吗?
我也尝试过结果,但它显示未定义的索引
您的代码容易受到 sql 注入的攻击。请了解如何在查询中使用准备好的语句。 websitebeaver.com/…
你试过echo $result[0]['access']
吗?
使用print_r($result);
正下方的$result=mysqli_fetch_array($run);
来获取存储的数据。用户print_r(error_get_last());
就在它下面,看看有没有连接错误什么的。
【参考方案1】:
我做了很多研究,终于自己找到了答案。
我使用 $_SESSION 和 select 查询从数据库中获取当前用户的信息。并使用 while 循环将它们放入变量中。 更新后的代码如下:
<div class="sing"><?php
session_start();
if (isset($_SESSION['email']) && $_SESSION['email'] == true)
echo "   Welcome " . $_SESSION['email'] ;
echo "<div style='float: right;'><a href='logout.php'>Logout</a> </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>
【讨论】:
以上是关于从数据库中检索值并将其存储为会话变量的主要内容,如果未能解决你的问题,请参考以下文章