PHP - Update

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
sup bois
sorry for spamming threads but I have more errors.
Whenever I click the fa fa-user-times icon, It should update column "is_vip_shop_staff" to 0.
But when I click the icon, it deletes All rows.
Code:
Code:
while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
$id = $row['id'];
echo "<tr><td>" . ltrim($row['id'], '0') . "</td><td>" . $row['username'] . "</td><td>" . $row['rank'] ."</td> ";
echo "<td><a href='?fire=$id'><i class='fa fa-user-times' aria-hidden='true'title='Fire user'></i></a></td>";
if(isset($_GET['fire'])) {
    mysql_query("UPDATE `users` SET `is_vip_shop_staff`='0' WHERE (`id`='$id')");
    echo "fired user successfully'";
    echo "<meta http-equiv='refresh' content='3;url=usermanagement.php'>";
}
}
Image:
W0Az5N.png
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
Couple of things wrong here:
  1. You've put your if statement inside your while loop, so each time it loops around a record, it's going to be checking that if statement every time when it only needs to do it once
  2. You're not actually checking the correct ID, you're checking $id which is the variable that comes out of the database around each loop iteration, not the ID from the URL
  3. If you put your if statement before the while loop, it will update the user's record before the while loop starts, so the record shouldn't reappear and you won't need to reload the page, of course you'll need to put your original SELECT query after the if statement
So here's a revised code that should hopefully work...

PHP:
<?php
if (isset($_GET['fire'])) {
    @mysql_query("UPDATE `users` SET `is_vip_shop_staff` = '0' WHERE `id` = '" . ((int)$_GET['fire']) . "'");
}
    
$query = "SELECT * FROM users WHERE `is_vip_shop_staff` = 1";
$result = mysql_query($query);
echo "<table class='table table-hover'><thead><tr><th>ID</th><th>Username</th><th>In-Game Rank</th><th>Fire user</th></tr></thead><tbody>";
    
    while ($row = mysql_fetch_array($result)) {
        echo "<tr><td>" . $row['id'] . "</td><td>" . $row['username'] . "</td><td>" . $row['rank'] ."</td> ";
        echo "<td><a href='?fire=" . $row['id'] . "'><i class='fa fa-user-times' aria-hidden='true'title='Fire user'></i></a></td>";
    }
    
//and then whatever else you have here, such as the ending </table> code
 

Users who are viewing this thread

Top