PHP-将$ _POST保存到$ _SESSION问题

时间:2016-03-23 15:22:52

标签: php mysql session

我已经在这里阅读了多篇帖子,但似乎没有一个可以解决这个问题,也许我只是误解,因为我是新手。 我有一个表单插入数据库,然后完美地回显数据!我的问题是因为表单在用户帐户页面上,当您注销所有信息时消失。我知道我必须将$ _POST变量保存到$ _SESSION。

但即使保存到会话中,一旦注销,数据回显仍然会消失。重新登录时。将$ _POST保存到$ _SESSION的正确方法是什么。

我目前正在使用:

// Save $_POST to $_SESSION
$_SESSION['fname'] = $_POST;

我的代码是否有更好的方法:

HTML

      <section class="container">
    <form id="myform " class="Form" method="post" action="Cus_Account.php?c_id=<?php echo $c_id ?>" accept-charset="utf-8">

        <!--                    <div id="first">-->
        <input type="text" id="fname" name="fname" value="<?php echo isset($_POST['fname']) ? $_POST['fname'] : '';?>" required> 
        <input type="text" id="lname" name="lname" value="<?php echo isset($_POST['lname']) ? $_POST['lname'] : '';?>" required>
        <input type="text" id="email" name="email" value="<?php echo $_SESSION['Cus_Email']; ?>" required>
        <input type="number" id="phone" name="phone"  value="<?php echo isset($_POST['phone']) ? $_POST['phone'] : '';?>"required>
        <input type="submit" name="Update" value="Update">
        <br>
    </form>

PHP

  <?php
if (isset($_POST['Update'])) {
$c_fname = $_POST['fname'];
$c_lname = $_POST['lname'];
$c_email = $_POST['email'];
$c_phone = $_POST['phone'];

// Save $_POST to $_SESSION
$_SESSION['fname'] = $_POST;
//query

$insert_det = "INSERT INTO Cus_acc_details(CUS_Fname,CUS_Lname,Cus_Email,CUS_Phone) VALUES (?,?,?,?)";
$stmt = mysqli_prepare($dbc, $insert_det);
//new
// $stmt = mysqli_prepare($dbc, $insert_c);
//debugging
//$stmt = mysqli_prepare($dbc, $insert_c)  or die(mysqli_error($dbc));

mysqli_stmt_bind_param($stmt, 'sssi', $c_fname, $c_lname, $c_email, $c_phone);

/* execute query */
$r = mysqli_stmt_execute($stmt);

// if inserted echo the following messges
if ($r) {
    echo "<script> alert('registration sucessful')</script>";
}
} else {
echo "<b>Oops! Your passwords do not </b>";
}
?>

表单中的$ _SESSION [&#39; Cus_Email&#39;]来自另一个查询。 任何帮助或建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

Templ<const X>数据应仅临时存储为会话变量。例如,如果您的用户出错:

form.php的

$_POST

action.php的

<?php
  // This function should go in a config file, to escape data:
  function html($str){
    return htmlspecialchars($str, ENT_QUOTES);
  }

  $data   = $_SESSION['form']['data'];
  $errors = $_SESSION['form']['errors'];
?>
<form method="post" action="action.php">

  <input type="text" name="fname" value="<?=html($data['fname'])?>" placeholder="First name">
  <?php if(isset($errors['fname'])): ?>
    <p>ERROR: <?=html($errors['fname'])?></p>
  <?php endif; ?>

  <input type="text" name="lname" value="<?=html($data['lname'])?>" placeholder="Last name">

  <button type="submit">Go</button>

</form>
<?php
  unset($_SESSION['form']); // You don't want to keep this data any longer.

您应该在数据库中存储名字,姓氏等内容。除了上面的示例之外,不要将它们存储在<?php $data = $_POST; // Validate the data, for example: if($data['fname'] == ''){ $errors['fname'] = "First name is required."; } if(!empty($errors)){ unset($data['password']); // Do not store passwords in session variables. $_SESSION['form']['data'] = $data; $_SESSION['form']['errors'] = $errors; header("Location: form.php"); die; } // Put your database inserts here (no errors) 中。