简体   繁体   中英

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

 <?php
            $sql = "SELECT * FROM houses"; 
            $q=$conn->query($sql);
           while ($row = mysqli_fetch_array($q)) {
?>
              <tr>
            <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>
            <td>
                              <div class="btn-group">
                              <?php 
                              $_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>
                              </div>
                              </td>
        </tr>
<?php
           }

        ?> 

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>
                          </div>
                          </td>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM