-2

I made a website with PHP. I did all my tasks but still have problem in this. I wrote this code to be able to delete multiple CheckBoxs in the Control Panel,

It works but only deletes one box.

function SelectDelete()
{
        $a = new mysqli("localhost", "root", "", "gamsite");
        if($a->connect_error)
        {
                die("something went worng".$a->connect_error);
        }
        $tmp=$a->query("SELECT * FROM game");
        if($tmp->num_rows>0)
        {
             while($record=$tmp->fetch_assoc())
             {
                ?>

<form  method="post">
<table class="table table-dark">

  <thead>
    <tr>
    <th scope="col"></th>
    <th scope="col"></th>
    <th scope="col"></th>
      <th scope="col">ID</th>
      <th scope="col">Name</th>
      <th scope="col">Download</th>
      <th scope="col">Image</th>
      <th scope="col">video</th>
      <th scope="col">subgame</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row"></th>
      <th scope="col"></th>
      <input type="hidden" name="act" value="SelectDelete">
      <td>
      <input type="checkbox" name="check[]" value="<?php echo $record['id']; ?>">
      </td>
      <td  name="id"><?php echo $record['id']; ?></td>
      <td name="name"><?php echo $record['name']; ?></td>
      <td name="download"><?php echo $record['download']; ?></td>
      <td name="image"><?php echo $record['image']; ?> </td>
      <td name="video"><?php echo $record['video']; ?></td>
      <td name="subgame"><?php echo $record['subgame']; ?></td>
      <th scope="col"><th scope="col">
    <button type="submit" name="delete" class="btn btn-danger" onclick="location.reload()">Delete</button>
    </th> 
    </tr>
   
  </tbody>
  
</table>
</form>

                 <?php
             }
        }
        else
        {
         echo "There is no data";
        }
if(isset($_POST['delete']))
{
        if(isset($_POST['check']))
        {
             foreach($_POST['check'] as $delete_id)
             {
                $a = new mysqli("localhost", "root", "", "gamsite");
                if($a->connect_error)
                {
                        die("something went worng".$a->connect_error);
                }
                $a->query("DELETE FROM game WHERE id='$delete_id'" );                  
             }   
        }
    }
}

I did try everything to make it work but nothing worked. Watched a couple of video in YouTube but I have to change my code structure .

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Ash Galmouz
  • 55
  • 1
  • 4
  • Do not make multiple database connections. Your script should only connect once and re-use that connection for all queries. Your delete query is insecure/unstable because you are not using a prepared statement. A mysqli result set object (`$tmp` in your case) can be immediately fed into a `foreach()` and treated as an index array of associative arrays without any `fetch` calls. https://stackoverflow.com/a/66775416/2943403 I recommend naming your connection variable more sensibly -- `$a` is not a good name for a connection. Pass the connection to your custom function `SelectDelete($conn)`. – mickmackusa Mar 30 '21 at 01:39
  • I also don't like the look of `onclick="location.reload()"` or ``. I reckon those should go away. I think you have a typo in your `die()` text -- should it be `smoethnig newt`? – mickmackusa Mar 30 '21 at 02:21

1 Answers1

0

You are starting your while loop on the entire form. This means that for each submit button there is only one check box. You should initiate your form in HTML, and then run your loop in order to have multiple check boxes.

As an example:

<form method="post">
    <?php while($record = $tmp->fetch_assoc()): ?>
        // Add the other HTML here, including your checkbox
        <input type="checkbox" name="check[]" value="<?=$record['id']?>">
    <?php endwhile; ?>
</form>

You should think about how you want this data to be displayed. As it currently stands, each form has only one checkbox, so only one will be deleted on submit.

Jesse
  • 2,790
  • 1
  • 20
  • 36