gpt4 book ai didi

从表单传递的 php SESSION 变量

转载 作者:行者123 更新时间:2023-12-04 05:26:28 24 4
gpt4 key购买 nike

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

我的表单使用 $_POST 将县名和语言作为两个单独的字段提供给以下脚本。该脚本在查询被硬编码时有效,并且在通过 $_POST 传递变量时对于 page=1 results.php 页面有效。但是,后续页面丢失了 $_POST 变量,我知道这个问题的解决方案涉及 $_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 *************************************/

我知道该代码在不使用 $_POST 传递参数的情况下也能正常工作,但我无法让它处理多个结果页面 (results.php?page=n)。我错过了什么?

问候,

鲍勃

最佳答案

您必须将 session_start() 放在每个使用 $_SESSION 变量的脚本的开头。

关于从表单传递的 php SESSION 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14501285/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com