-1

Im working on an older project. that needs a small fix with deleting a specific row from the database. There is a loop that retrieves images from the database and adds a delete button to it as well. but when i click the remove button it removes all the images instead of the image i click on. so if it retrieves 5 images. it removes 5 instead of 1 that i click on. ps.(don't hate about the older version of sql that's used pls :)

$result = mysqli_query($con, "SELECT selfies.ID, selfies.afbeelding FROM selfies");
while($row = mysqli_fetch_array($result)){
    echo "<div class='results'>";
    $image = $row['afbeelding'];
    $image_src = "images/".$image;
    echo "<img class='selfiess' src='$image_src'>";
    echo "<br>";
    echo "<form method='post'><button type='submit' value='".$row['ID']."' class='button special big' name='removeSel'>Remove</button></form><br><br>";
    echo "</div>";
    if (isset($_POST['removeSel'])){
        $query = mysqli_query($con, "DELETE FROM selfies WHERE ID='".$row['ID']."' LIMIT 1");
        $result2 = mysql_query($query);
        header("Location: index.php?page=selfies");
    }
}
  • Post your js code – Saad Suri Nov 12 '18 at 10:44
  • you have write the delete query inside the while loop and also using `$row['ID']` . that's mean all images will be deleted . you should write delete query outside the loop and get image id from `form submit` – Bilal Ahmed Nov 12 '18 at 10:46
  • Possible duplicate of [Can I mix MySQL APIs in PHP?](https://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – Alon Eitan Nov 12 '18 at 10:46
  • The above dupe is for mixing `mysql_query` + `mysqli_query` inside the `if`, and why are you not deleting `$_POST['removeSel']`outside the loop instead of deleting all the records that you fetch – Alon Eitan Nov 12 '18 at 10:47
  • Show your html code and jquery or js code also. – Leena Patel Nov 12 '18 at 11:04

1 Answers1

-1

As per my comment...(you have write the delete query inside the while loop and also using $row['ID'] . that's mean all images will be deleted . you should write delete query outside the loop and get image id from form submit)

$result = mysqli_query($con, "SELECT selfies.ID, selfies.afbeelding FROM selfies");
while($row = mysqli_fetch_array($result)){
    echo "<div class='results'>";
    $image = $row['afbeelding'];
    $image_src = "images/".$image;
    echo "<img class='selfiess' src='$image_src'>";
    echo "<br>";
    echo "<form method='post'><input type='hidden' name'image_id' value='".$row['ID']."'><button type='submit' value='".$row['ID']."' class='button special big' name='removeSel'>Remove</button></form><br><br>";
    echo "</div>";

}
//here is delete image code outside of loop
        if (isset($_POST['removeSel'])){
            $query = mysqli_query($con, "DELETE FROM selfies WHERE ID='".$_POST['image_id']."' LIMIT 1");
            $result2 = mysql_query($query);
            header("Location: index.php?page=selfies");
        }
Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42