dongshuzhuo5659 2015-01-28 08: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 08: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'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部