dongshuzhuo5659 2015-01-28 16:43
浏览 62
已采纳

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

Here's my case (relatively new on php): I got a page "zoek_form.php" where you can enter 2 search values in a form (naam and categorie). When submitted, page "zoek.php" is loaded and a search will be performed (mysql 5.6). To perform the search the 2 values are obtained from the 2 session variables. So far so good, the search works and the rows are retrieved. But now I want the user to be able to make a sequence (via ORDER BY) in zoek.php, based on a dropdown list. The selected value will be stored in a 3rd session variable. But now the problem: when selecting a sequence and submit the form, the first 2 session values are lost. I'm puzzled why. The essence of session variables is just storing the values and to be able to use them over and over again? (till they are overwritten or killed). Of course I use session_start(); at the beginning of the php-script (otherwise it would not have worked at all ;-). Any ideas?

Here's 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>

Here's 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>
  • 写回答

1条回答 默认 最新

  • duanran3115 2015-01-28 16:46
    关注

    Your form in zoek.php doesn't contain form_naam and form_cat so when you run

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

    It sets those values to null. If you want to retain those values you could try passing them back again in the form with hidden input fields

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

    Another way to prevent overwriting the session values is to only change them if the $_POST values are set

    if(isset($_POST['form_naam']) && isset($_POST['form_cat'])) {
      $_SESSION['form_naam'] = $_POST['form_naam'];
      $_SESSION['form_cat'] = $_POST['form_cat'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码