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'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)