3

I have a form that enables user to select several items:

<table>
<form name="delete" action="proceed.php" method="post">
<?php 
 require_once 'cnn.php';
 $viewPhoto = mysqli_query($cnn, "SELECT * FROM competition WHERE round = 2 ORDER BY id Desc");


                while($row = mysqli_fetch_array($viewPhoto)) {    ?>

<tr><td><input type="checkbox" name="check[]" value="<?php echo $row['id']; ?>"></td>
<td><?php echo $row['id']; ?></td>
<td><img title="" src="<?php echo $row['url']; ?>"  width="178" height="150" /></td></tr>                   

<?php } ?>
<input type="submit" name="delete" value="Delete items">
</form>
</table>

The proceed.php then includes:

<?php
 require_once 'cnn.php';
 $sql = mysqli_query($cnn, "SELECT * FROM competition");

 if(isset($_POST['delete'])){
   foreach ($_POST["check"] as $id){
   $sql = "DELETE FROM soutez WHERE id='$id'";
}
   echo "Items deleted";
}
?>

However, the items are not deleted from database. Where could be the mistake?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jan Mares
  • 57
  • 9
  • Are you getting any error? Also mention where you could stucked? – Ranjith Jun 16 '14 at 06:25
  • hint: there is no `mysqli_query()`. you did not execute it. tip: use parameterized queries :) – user1978142 Jun 16 '14 at 06:25
  • 1
    Maybe this is relevant: You are selecting from "competition" and deleting from "soutez". –  Jun 16 '14 at 06:27
  • @JeremyMiller : How do you say that? User doesn't share any `DB schema`. Is there any rules `where` we select, on that table record only can able to delete? – Ranjith Jun 16 '14 at 06:31
  • Jeremy: It was just a mistake in my translation: competition = soutez in Czech :) – Jan Mares Jun 16 '14 at 06:33
  • OK. I was just reading the code and read the statements. No idea where Rangith's question comes from b/c you can clearly see in the question the different names. –  Jun 16 '14 at 06:34
  • @kevinabelita Should not be the row in proceed.phpb "$sql = mysqli_query($cnn, "SELECT * FROM competition");" enough? – Jan Mares Jun 16 '14 at 06:35
  • @JanMares im not referring on the select. im referring on the delete. check out [`mysqli_bind_param()`](http://jp2.php.net//manual/en/mysqli-stmt.bind-param.php) for safer queries. its not safe directly inserting post values on your query. – user1978142 Jun 16 '14 at 06:42
  • Thanks @kevinabelita. Already found the solution in answer below. – Jan Mares Jun 16 '14 at 06:44

1 Answers1

4

You are missing executing actual query in loop for deleting record

<?php
 require_once 'cnn.php';
 $sql = mysqli_query($cnn, "SELECT * FROM soutez");

 if(isset($_POST['delete'])){
   foreach ($_POST["check"] as $id){
   $sql = "DELETE FROM soutez WHERE id='$id'";
   //Here you are missing below statement
   mysqli_query($cnn, $sql);
}
   echo "Items deleted";
}
?>
Sadikhasan
  • 18,365
  • 21
  • 80
  • 122