0
 $result = mysqli_query($conn,"select * from `favourites` WHERE user_id = '$session'");
    while($row = mysqli_fetch_assoc($result)) { ?>

      <input type="hidden" value="<?php echo $row['id']; ?>" name="name"/>

       <?  $pid=$row['id'];

    }

    if(isset($_POST['delete'])) {
     $delete="DELETE FROM `favourites` WHERE id = '$pid' AND user_id = '$session'";
        if (mysqli_query($conn, $delete)) {
            echo "Record deleted successfully " . $id;
        } else {
            echo "Error deleting record: " . mysqli_error($conn);
        }
}

So the story of this page is to display all the images witch are favoured by the user (Witch works).Now of course if user wants to remove them,i added "delete" button.But the problem is,It only deletes the last picture when i press the button on any of the pictures.For example,i have pictures with IDs : 10,11,12,13...and they are displayed in that order too.So when i press delete on for exmp picture 11,it will delete picture 13.So basicly no matter what picture i want to remove from table,it always removes the last one. Iv put hidden button in while loop to get the pictures ID,and set a delete condition in If statment to delete when that id is equal to the id in the table.

Any ideas what am i doing wrong ?

Edit-The delete form :

    <form method="post">
<input type="hidden" value="<?php echo $row['name']; ?>" name="name"/>
<input type="hidden" value="<?php echo $row['id']; ?>" name="nameid"/>
<button type="submit"  class="btn btn-danger btn-circle" name="delete"><i class="glyphicon glyphicon-remove"></i></button>

                                        </form>
virtuon92
  • 49
  • 5
  • Where is the code generating html ? To see how 'delete' is filled... – Random Apr 05 '16 at 12:56
  • please show delete button code of html. – RJParikh Apr 05 '16 at 12:57
  • I added/edited delete form i use – virtuon92 Apr 05 '16 at 13:03
  • 1
    [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). – Jay Blanchard Apr 05 '16 at 13:05

2 Answers2

1
DELETE FROM `favourites` WHERE id = '$pid'

this $pid will have the last id after the while loop will end,thereby only deleting the last one

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
WebInsight
  • 307
  • 2
  • 20
1

Your form is posting the ID of the pic to be deleted having name "nameid". so in delete query use the ID posted.

if(isset($_POST['delete'])) {
 $delete="DELETE FROM `favourites` WHERE id = '".$_POST['nameid']."' AND user_id = '$session'";
    if (mysqli_query($conn, $delete)) {
        echo "Record deleted successfully " . $_POST['nameid'];
    } else {
        echo "Error deleting record: " . mysqli_error($conn);
    }
}
D.k
  • 442
  • 4
  • 13
  • Why should the OP "try this"? A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Apr 05 '16 at 13:05