henry henry - 9 months ago 33
MySQL Question

Select a table row from an action button in PHP

i presently have a table which is auto populated from my database, i have three action buttons connected to each row which are edit, delete, and view. Am presently using session to send my key value to the next page, so that i can use that key value to load that particular row's data. the problem am having is that my session is picking the unique id of the last row instead of the row that i selected. here is my code

$sql = "SELECT * FROM houses";
while ($row = mysqli_fetch_array($q)) {
<td><?php echo $row['nickname']; ?></td>
<td><?php echo $row['state']; ?></td>
<td><?php echo $row['city']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['house_type']; ?></td>
<td><?php echo $row['owner']; ?></td>
<td><?php echo $row['price']; ?></td>
<div class="btn-group">
$_SESSION['house'] = $row['nickname'];
echo $_SESSION['house'];
<a class="btn btn-primary" href="edit_property.php"><i class="icon_plus_alt2"></i></a>
<a class="btn btn-success" href="view_property.php"><i class="icon_check_alt2"></i></a>
<a class="btn btn-danger" href="delete_property.php"><i class="icon_close_alt2"></i></a>



That's because you don't want to use sessions in this case, the browser must give you back the ID that you want to view or edit.

Change it like this:

                          <div class="btn-group">
                              <a class="btn btn-primary" href="edit_property.php?house=<?php echo $row['nickname']; ?>"><i class="icon_plus_alt2"></i></a>
                              <a class="btn btn-success" href="view_property.php?house=<?php echo $row['nickname']; ?>"><i class="icon_check_alt2"></i></a>
                              <a class="btn btn-danger" href="delete_property.php?house=<?php echo $row['nickname']; ?>"><i class="icon_close_alt2"></i></a>

Then you will find in $_GET['house'] the key to use, not in $_SESSION