簡體   English   中英

從數據庫中檢索值並將其存儲為會話變量

[英]retreive values from database and store it as session variable

我已經在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> 

用戶電子郵件在表單中正確輸入,但手機號碼和其他詳細信息未添加到表單中。 我嘗試將它們串起來作為上面的變量,但它沒有顯示。 我想我需要從數據庫中獲取詳細信息。 誰能告訴我怎么做?

我做了很多研究,最后自己回答了問題。

我使用$ _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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM