IRHM
IRHM

Reputation: 1326

Session Variable No Longer Working

I wonder whether someone may be able to help me please.

I feel I need a fresh pair of eyes to look at a problem I have which I can't see a solution to.

The form below is used to list Location records held in a MySQL database.

    <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>

Using the code below, the four buttons at the end of my form take the user to four separate screens, all linked back to the Location record via the $lid variable.

<?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);
}
?>

All of the four receiving forms therefore have this $lid = $_SESSION['lid']; at the top of each page to capture this variable.

This architecture worked perfectly up to a few days ago, and it's now all gone awry and I'm not sure why. I know that this field <input type="text" id="lid" name="lid" value="<?=$lid?>"/> in my form is holding the correct value.

However, irrespective of how many Locations records there are, if I click on any button I'm taken to the correct receiving screens, but it's always for the last 'Location` record in the list.

I just can't seem to find what I'm doing wrong here, or understand why it's now started to cause me problems. I've checked the JavaScript console in Chrome, and that's not showing me any errors either.

I just wondered whether someone could perhaps take a look at this please an let me know where I'm going wrong.

Many thanks and kind regards

Upvotes: 0

Views: 140

Answers (2)

Donatas Olsevičius
Donatas Olsevičius

Reputation: 1350

All of your "lid" inputs are in the same form. When you submit your form, all values are sent and only the last one get into your $_POST array. Move form tags to the inside of your loop (that means generate many forms) ant it should work fine.

<?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
}

Upvotes: 1

Max Hudson
Max Hudson

Reputation: 10206

You have a typo:

'mysql_numrows'

Should be:

'mysql_num_rows'

Upvotes: 1

Related Questions