Skip to content

Session in While Loop PHP

<form action="includes/xxx.php"  method="get">
                <td><?php echo $row['id']; 
                        $_SESSION['id'] = $row['id'];

                <td><?php echo $row['length']; ?></td>
                <td><?php echo $row['name']; ?></td>
                <td><?php echo $row['value'];
                    $_SESSION['value'] = $row['value'];

                <td><input type="text" name="value" placeholder="XXX"></td> 

                <td><button type="submit" name="submit" value="submit">  OK </button> </td>

In the upper code, I’m displaying my db table with a while loop


I get a value from the user, and I want to add it with the value in the same row and then update my db table.

GET method / displaying / SESSIONS and Update are done, but when I’m filling the input field the result saved in the last id.

I know that my code overwrites something and it can’t understand what input I filled up.

How can I solved it?



Don’t use sessions this way. Add a separate hidden input box to send the ID to the server for processing.

    <input type="hidden" name="row_id" value="<?php echo $row['id']; ?>">
    <input type="text" name="value" placeholder="XXX">

Now when you submit the form you will have 2 values, $_GET['row_id'] and $_GET['value']. Use the row_id to update the row you are trying to change.

If needed, you can still set the session values when you are processing the submitted form.

User contributions licensed under: CC BY-SA
3 People found this is helpful