lundi 29 mars 2021

Submit single form with multiple checkboxes

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 .




Aucun commentaire:

Enregistrer un commentaire