从表单传递的php会话变量

从表单传递的php会话变量,php,session-variables,Php,Session Variables,我在尝试将表单变量传递给生成多个页面的脚本时遇到了一个问题 我的表单提供了一个县名和一种语言作为两个单独的字段,使用$\u POST将其添加到以下脚本中。当查询是硬编码时,脚本可以工作;对于page=1 results.php页面,当通过$\u POST传递变量时,脚本可以工作。但是,随后的页面会丢失$\u POST变量,我知道这个问题的解决方案涉及到一个$\u SESSION命令,但我无法让它工作:(。事实上,现在没有显示结果,尽管参数被传递给了这些函数 这是我的密码 <?php $_S

我在尝试将表单变量传递给生成多个页面的脚本时遇到了一个问题

我的表单提供了一个县名和一种语言作为两个单独的字段,使用$\u POST将其添加到以下脚本中。当查询是硬编码时,脚本可以工作;对于page=1 results.php页面,当通过$\u POST传递变量时,脚本可以工作。但是,随后的页面会丢失$\u POST变量,我知道这个问题的解决方案涉及到一个$\u SESSION命令,但我无法让它工作:(。事实上,现在没有显示结果,尽管参数被传递给了这些函数

这是我的密码

<?php
$_SESSION['county']=$_POST['county'];
$_SESSION['language']=$_POST['language'];

$speak=$_POST['language'];
$area=$_POST['county']
?>
<table class="bda" width="100%" align = "center"><tr><td class="hrak">Open test db file on lcn</td></tr></table>

<?php
$conn = connect();
/*******************************************************************************************
The variable '$showrecs' refers to the number of rows on each page - this can be changed.
'$pagerange refers to blocks of records, again can be changed.
*******************************************************************************************/
 $area=$_POST['county'];
 $speak=$_POST['language'];
 $showrecs = 20;
 $pagerange = 10;
 $page = @$_GET["page"];
 if (!isset($page)) $page = 1;
 select();
 mysql_close($conn);
 ?>
 <table class="bda" width="100%"><tr><td class="hrak">Powered by MySQL</td></tr></table>
 </body>
 </html>
 <?php
 /******************************************** - START OF FUNCTIONS  *************************************/
 ?>
 <?php function select()
  {
  global $a;
  global $showrecs;
  global $page;

  $res = sql_select();
$count = sql_getrecordcount();
  if ($count % $showrecs != 0) {
      $pagecount = intval($count / $showrecs) + 1;
  }
  else {
      $pagecount = intval($count / $showrecs);
  }
      $startrec = $showrecs * ($page - 1);
  if ($startrec < $count) {mysql_data_seek($res, $startrec);}
  $reccount = min($showrecs * $page, $count);
  ?>
     <table class="bd" border="0" cellspacing="1" cellpadding="4" width="100%" align = "center">

     <tr><td class="hrak" align="center" width="100%">Records shown <?php echo $startrec + 1 ?> -        <?php echo $reccount ?> of <?php echo $count ?></td></tr>
    </table>

 <?php showpagenav($page, $pagecount); ?>
 <br>
 <table class="tbl" border="0" cellspacing="1" cellpadding="5"width="100%">
 <tr>

 <!- ***************************************
    HEADER row of the table - HTML
  **************************************** ->

  <td>
  </td>

  </tr>
<?php
   for ($i = $startrec; $i < $reccount; $i++)
   {
     $row = mysql_fetch_assoc($res);
     $mem13 = ($row["title"]);
     if ($mem13 == 'Mr')
         {
         $style = "dr";
         $styler = "drr";

      if ($i % 2 != 0) 
         {
         $style = "sr";
         $styler = "srr";
         }
    }
   else
    {
         $style = "ndr";
         $styler = "ndrr";

      if ($i % 2 != 0) 
    {
        $style = "nsr";
        $styler = "nsrr";
    }

   }
?>

<tr>

 <!-- *********************************************
This is where I populate the DATA rows of the table  - HTML
 ********************************************** -->


 <td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["id"]) ?></td>
 </tr>

<?php
    }
   /*** mysql_free_result($res); ***/
 ?>
</table>
<br>
<?php showpagenav($page, $pagecount); ?>
<?php } ?>

<?php function showpagenav($page, $pagecount)
{
?>
  <table class="bd" border="0" cellspacing="1" cellpadding="4" >
  <tr>
<?php if ($page > 1) { ?>
 <!- /*************************************************************************
 This generates 'results.php' and the sub pages results results.php?page=2...
 *****************************************************************************/
 -->

 <td class="hrak1" align="center"><a href="results.php?page=<?php echo $page - 1 ? >">&lt;&lt;&nbsp;Prev</a>&nbsp;</td>
<?php } ?>
<?php
   global $pagerange;

   if ($pagecount > 1) {

   if ($pagecount % $pagerange != 0) {
     $rangecount = intval($pagecount / $pagerange) + 1;
   }
   else {
      $rangecount = intval($pagecount / $pagerange);
        }
  for ($i = 1; $i < $rangecount + 1; $i++) {
     $startpage = (($i - 1) * $pagerange) + 1;
     $count = min($i * $pagerange, $pagecount);

     if ((($page >= $startpage) && ($page <= ($i * $pagerange)))) {
       for ($j = $startpage; $j < $count + 1; $j++) {
        if ($j == $page) {
 ?>
  <td><b><?php echo $j ?></b></td>
 <?php } else { ?>
 <!--
 /*****************************************************************************************************
 results.php?pagen  so that the required page number is generated.
 *****************************************************************************************************/
-->
<td class="hrak1" align="center"><a href="results.php?page=<?php echo $j ?>"><?php echo $j ?></a></td>
<?php } } } else { ?>
<td class="hrak1" align="center"><a href="results.php?page=<?php echo $startpage ?>"><?php echo  $startpage ."..." .$count ?></a></td>
<?php } } } ?>
<?php if ($page < $pagecount) { ?>
<td class="hrak1" align="center">&nbsp;<a href="results.php?page=<?php echo $page + 1 ?>">Next&nbsp;&gt;&gt;</a>&nbsp;</td>
<?php } ?>
</tr>
</table>
<?php } ?>

<?php 

/******************************************** - FUNCTION - SQL_SELECT *************************************/
function sql_select()
{
/******************************************************************************
SELECT statement for results to be displayed
******************************************************************************/
   $area=$_POST['county'];
   $speak=$_POST['language'];
   global $conn;
   $sql = "SELECT * FROM test2 WHERE language='$speak' AND county='$area' ORDER BY id";
   $res = mysql_query($sql, $conn) or die(mysql_error());
   return $res;
}
/******************************************** - FUNCTION - SQL_GETRECORDCOUNT *************************************/
function sql_getrecordcount()
{
  $area=$_POST['county'];
  $speak=$_POST['language'];
  $sql = "SELECT COUNT(*) FROM test2 WHERE language='$speak' AND county='$area'";
    $res = mysql_query($sql, $conn) or die(mysql_error());
    $row = mysql_fetch_assoc($res);
    reset($row);
    return current($row);
}
/******************************************** - FUNCTION - CONNECT *************************************/

在lcn上打开测试数据库文件

您必须在每个使用
$\u session
变量的脚本开头放置
session\u start()

session start()
添加到使用会话变量的所有页面


阅读手册试试这个,我希望它能起作用:-

session_start();
$_SESSION['county']=$_POST['county'];
$_SESSION['language']=$_POST['language'];

是否在不启动会话的情况下使用$\u会话变量?
在脚本中使用
session_start();

感谢您的输入。我通过在调用页面上设置cookie来解决此问题,然后在结果页面上使用cookie:

<?php
    $area  = $_COOKIE['Carea'];
    $speak  = $_COOKIE['Cspeak'];
?>
它是有效的


Bob

大家好,我使用了session_start();但没有任何结果。很抱歉,我在代码中遗漏了此内容,但在发布后发现了错误。
function sql_select($area,$speak)