调用第 3 个会话变量后丢失前 2 个会话变量 (php 5)

Losing first 2 session variables after calling a 3rd session variable (php 5)

这是我的案例(在 php 上相对较新):我有一个页面 "zoek_form.php",您可以在其中以表格形式输入 2 个搜索值(naam 和 categorie)。提交后,将加载页面 "zoek.php" 并执行搜索 (mysql 5.6)。要执行搜索,需要从 2 个会话变量中获取 2 个值。到目前为止一切顺利,搜索有效并检索到行。 但现在我希望用户能够根据下拉列表在 zoek.php 中创建一个序列(通过 ORDER BY)。所选值将存储在第三个会话变量中。但是现在问题是:选择序列并提交表单时,第一个2个会话值会丢失。我很困惑为什么。会话变量的本质只是存储值并能够一遍又一遍地使用它们? (直到它们被覆盖或杀死)。 我当然用 session_start();在 php-脚本的开头(否则它根本无法工作 ;-)。有什么想法吗?

这里是zoek_form.php:

<html>
<head>
<title>Zoeken</title>
</head>
<body>

<?php session_start(); ?>
<form name="form1" method="POST" action="zoek.php">
<table border="0">
<tr><td>Naam product:</td>
<td><input type="text" size="50" name="form_naam"></td></tr>
<tr><td>Categorie:</td>
<td><input type="text" size="50" name="form_cat"></td></tr>
<tr><td></td>
<td align = "right"><input type="submit" name="B1" value="Zoeken">
</td></tr>
</table>
</form>

</body>
</html>

这里是zoek.php:

<html>
<head>
<title>Zoeken</title>
</head>
<body>

<form name="form1" method="POST" action="">
<table border="0">
<tr><td>Sorteer op:</td>
<td><select name="form_sort">
  <option value="Naam">Naam</option>
  <option value="Categorie">Categorie</option>  
</select></td>
<td><input type="submit" name="B1" value="Sorteer"></td></tr>
</table>
</form>

<?php

session_start();
require_once 'test_connect.php';
$_SESSION['form_naam'] = $_POST['form_naam'];
$_SESSION['form_cat'] = $_POST['form_cat'];
$_SESSION['form_sort'] = $_POST['form_sort'];
// The 3 lines below were used to check whether session vars were set
// echo  $_SESSION['form_naam'];
// echo  $_SESSION['form_cat'];
// echo  $_SESSION['form_sort'];

function sorteren() {
global $sorteer;
$sorteer = $_SESSION['form_sort'];
  if ($sorteer == "Naam") {
  $sorteer = "ORDER BY naam";
  }
  else {
  $sorteer = "ORDER BY categorie";
  }
 }

// Put values from zoek_form.php in vars.
$naam = $_SESSION['form_naam'];
$cat = $_SESSION['form_cat'];

// Check if user has set a sequence. If yes: call function sorteren(),
// if no: leave var $sorteer empty.
if (isset($_SESSION['form_sort'])) {
sorteren();
}
else {
$sorteer = "";
}

// Get rows from table product
$sql = "SELECT * FROM product WHERE naam LIKE '$naam%' OR categorie
LIKE '$cat%' $sorteer";
$result = $conn -> query($sql);

if ($result->num_rows > 0) {
// here code to retrieve rows etc.    
}

// Give result free
$result -> free();

// Close connection
$conn -> close();

?>

</body>
</html>

您在 zoek.php 中的表单不包含 form_naamform_cat 所以当您 运行

$_SESSION['form_naam'] = $_POST['form_naam'];
$_SESSION['form_cat'] = $_POST['form_cat'];

它将这些值设置为空。如果您想保留这些值,您可以尝试以隐藏输入字段的形式再次将它们传回

<input type="hidden" name="form_naam" value="<?= $_SESSION['form_naam'] ?>">
<input type="hidden" name="form_cat" value="<?= $_SESSION['form_cat'] ?>">

防止覆盖会话值的另一种方法是仅在 $_POST 值已设置时才更改它们

if(isset($_POST['form_naam']) && isset($_POST['form_cat'])) {
  $_SESSION['form_naam'] = $_POST['form_naam'];
  $_SESSION['form_cat'] = $_POST['form_cat'];
}