Home > OS >  Problem with checkbox delete data for php database
Problem with checkbox delete data for php database

Time:09-16

I'm really new to the PHP SQL language and I'm really confused about my code to how to delete data using checkboxes after it has been searched. Here is my code I hope someone could help me. When I try to run the code it shows the page but when I try to delete the checked row it won't delete or the delete button won't actually work. Thank you!

    <head>
        <title>Search Courses</title>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <center>
        <div class="container"> 
        <form method="GET" action="search.php">
            <h3>Search Courses</h3>
            <input type="text" name="keyword" required placeholder="Type any keyword" />
            <input type="submit" name="search" value="Search" />
        </form>

        <form method="GET" action="search.php">
            <h3>Search by Status</h3>
            <select name="status">
            <option value="A">Active</option>
            <option value="I">Inactive</option>
            </select>
            <input type="submit" name="search" value="Search" />
        </form>
<?php
    if(isset($_GET["search"]))
    {
        $keyword = $_GET["keyword"];
        $con = mysqli_connect("localhost", "root", "", "school");
        if($con)
        {
            $query = "select * from program where acronym like '%$keyword%' or description like '%$keyword%' order by acronym ";
            $result = mysqli_query($con, $query);
            
            if(mysqli_num_rows($result) > 0)
            {
                echo "<table border='1' width='100%'>";
                echo "  <thead>";
                echo "      <tr>";
                echo "          <th>Acronym</th>";
                echo "          <th>Description</th>";
                echo "          <th>Status</th>";
                echo "          <th>Delete</th>";
                echo "      </tr>";
                echo "  </thead>";
                echo "  <tbody>";
                
                while($row = mysqli_fetch_assoc($result))
                {

                    echo "<tr>";
                    echo "  <td>".strtoupper($row['acronym'])."</td>";
                    echo "  <td>".$row['description']."</td>";
                    echo "  <td>".strtoupper($row['status'])."</td>";
                    echo "  <td>".'<input type="checkbox" value="'.$row['id'].'" name="delete[]"/>'."</td>";
                    echo "</tr>";
                    
                    
                }

                if(isset($GET['delete[]']))
                {
                    $selected = $GET['delete[]'];
                    $id = $_GET['delete[]'];
                    for($i=0; $i<count($selected); $i  )
                    {
                   $query = "DELETE FROM 'program' WHERE id = '".$selected[$i]."'";
                   mysqli_query($GLOBALS['link'], $query);
                    }
                    echo "Successfully deleted";
                }
                else
                {
                echo "Error";
                }
                
                echo "  </tbody>";
                echo "</table>";
                echo "<td>".'<input type="submit" name="delete[]" value="Delete"/>'."</td>";
            }
            else 
            {
                echo "<p>No records found.</p>";
            }


        }
        else 
        {
            echo "<p>Error DB</p>";
            
        }
        
    }
?>      
         
        </div>
    </body>
</html>
`

CodePudding user response:

This is the correct way to delete multiple items.

if(isset($_GET['delete'])){
    foreach($_GET['delete'] as $selectedid){
      $deletequery = mysqli_query($con, "DELETE FROM 'program' WHERE id = ".$selectedid);
      if($deletequery) {
         echo "Item is deleted"; 
      }
    }
  }
  •  Tags:  
  • php
  • Related