会话变量不再工作


Session Variable No Longer Working

我想知道是否有人可以帮助我。

我觉得我需要一双新鲜的眼睛来看待我遇到的一个我看不到解决方案的问题。

下面的表格用于列出MySQL数据库中保存Location records

    <form name="locationsconsole" id="locationsconsole" method="post" action="locationsaction.php">
                                    <?php
                                        $query = "SELECT * FROM `table` WHERE `userid` = '$idnum' ORDER BY locationname";
                                        $result = mysql_query($query) or die('error');
                                        $num = mysql_numrows($result);
                                        mysql_close($connect);                  
                                        $i=0;
                                        while ($i < $num) {
                                        $lid=mysql_result($result,$i,"locationid");
                                        $lname=mysql_result($result,$i,"locationname");
                                        $laddress=mysql_result($result,$i,"returnedaddress");
                             <tr>
                                <td><input type="text" id="lid" name="lid" value="<?=$lid?>"/></td>
                                <td><div align="center"><?php echo $lname;?></div></td>
                                <td><div align="left"><?php echo $laddress;?></div></td>
                                <td><div align="center"><?php echo $findscount?></div></td>
                                <td><div align="center"><input name="type" type="submit" value="View Details"/></div></td>
                                <td><div align="center"><input name="type" type="submit" value="Add Finds"/></div></td>
                                <td><div align="center"><input name="type" type="submit" value="Add Images"/></div></td>
                                <td><div align="left"><input name="type" type="submit" value="View Location Finds"/></div></td>
                            </tr>
    </form>

使用下面的代码,表单末尾的四个按钮将用户带到四个单独的屏幕,所有屏幕都通过$lid variable链接回Location record

<?php session_start();
$_SESSION['lid'] = $_POST['lid'];
if (isset($_POST['type'])) {
    $urls = array(
        'View Details' => 'viewlocation.php',
        'Add Finds' => 'addfinds.php',
        'Add Images' => 'addimages.php',
        'View Location Finds' => 'locationfinds.php'
    );
    $url = $urls[$_POST['type']];
    header("Location: " . $url);
}
?>

因此,所有四个接收表单在每个页面的顶部都有此$lid = $_SESSION['lid'];来捕获此变量。

这个架构在几天前运行良好,现在一切都出错了,我不知道为什么。我知道我的表单中<input type="text" id="lid" name="lid" value="<?=$lid?>"/>的这个字段保存正确的值。

但是,无论有多少Locations records,如果我单击任何按钮,我都会被带到正确的接收屏幕,但它始终是列表中最后一个"位置"记录。

似乎找不到我在这里做错了什么,或者理解为什么它现在开始给我带来问题。我已经检查了Chrome中的JavaScript控制台,也没有显示任何错误。

只是想知道是否有人可以看看这个,请让我知道我哪里出错了。

非常感谢和亲切的问候

您所有的"盖子"输入都是相同的形式。当您提交表单时,将发送所有值,只有最后一个值进入 $_POST 数组。将表单标签移动到循环内部(这意味着生成许多表单)它应该可以正常工作。

<?php
$query = "SELECT * FROM `table` WHERE `userid` = '$idnum' ORDER BY locationname";
$result = mysql_query($query) or die('error');
$num = mysql_num_rows($result);
mysql_close($connect);
$i = 0;
while ($i < $num) {
    $lid=mysql_result($result,$i,"locationid");
    $lname=mysql_result($result,$i,"locationname");
    $laddress=mysql_result($result,$i,"returnedaddress");
    ?><form name="locationsconsole" id="locationsconsole" method="post" action="locationsaction.php">
        <table>
            <tr>
                <td><input type="text" id="lid" name="lid" value="<?=$lid?>"/></td>
                <td><div align="center"><?php echo $lname;?></div></td>
                <td><div align="left"><?php echo $laddress;?></div></td>
                <td><div align="center"><?php echo $findscount?></div></td>
                <td><div align="center"><input name="type" type="submit" value="View Details"/></div></td>
                <td><div align="center"><input name="type" type="submit" value="Add Finds"/></div></td>
                <td><div align="center"><input name="type" type="submit" value="Add Images"/></div></td>
                <td><div align="left"><input name="type" type="submit" value="View Location Finds"/></div></td>
            </tr>
        </table>
    </form><?php
}

您有一个拼写错误:

"mysql_numrows"

应该是:

"mysql_num_rows"