0

So, here I have a table that shows the data in the MySQL Database. I have attached checkbox in the first row which selects the names to be deleted which are unique. How can I delete checked data in the form from Database using array or any other ways in PHP.

Here's my code,

My PHP script to show data,

            <form method='POST' action='../../panel/delete_others.php'>
          <table id='example1' class='table table-bordered table-hover'>
            <thead>
            <tr>
              <th>Mark User </th>
              <th>User name</th>
              <th>User Password</th>
              <th>User Type</th>
              <th>User Status</th>
            </tr>
            </thead>
            <tbody>";
            while($row = mysqli_fetch_array($result2)){
           echo "<tr>
              <td> <input type='checkbox' name='others_delete[]' value='".$row['USER_NAME']."'/></td>
              <td>".$row['USER_NAME']."</td>
              <td>".$row['USER_PASSWORD'].
              "</td>
              <td>".$row['USER_type']."</td>
              <td>".$row['USER_STATUS']."</td>
            </tr>";
            }
            echo "</tbody>
          </table>
          </form>

My PHP script to delete data,

<?php
include('config.php');
if(isset($_POST['submit'])){//to run PHP script on submit
    if(!empty($_POST['others_delete'])){
        foreach($_POST['others_delete'] as $selected){
            $sql ="DELETE FROM users WHERE USER_NAME='$selected' and USER_type='Others'";
            echo $selected;
            if(mysqli_query($db, $sql)){
                mysqli_query($db, $sql);
            }
             else{
                 echo "error: ".$sql. "<br>" . mysqli_error($db);
            }

            }
        }
    }
mysqli_close($db);

?>

So, the code i written seem right but whenever i hit submit button i get blank page.

Ramganesh
  • 106
  • 1
  • 11
  • You're already using an API that supports **prepared statements** with bounded variable input, you should utilize parameterized queries with placeholders (prepared statements) to protect your database against [SQL-injection](http://stackoverflow.com/q/60174/)! Get started with [`mysqli::prepare()`](http://php.net/mysqli.prepare) and [`mysqli_stmt::bind_param()`](http://php.net/mysqli-stmt.bind-param). – Qirel Sep 24 '17 at 11:40
  • 3
    If you get a blank page, it's either because no condition was true, or you have a fatal error. If its the latter, check your logs. If its the first, check which conditions fail by adding `else` and some output (or log something). – Qirel Sep 24 '17 at 11:41
  • the blank page most likly suggest there is a disabled error.. please enable `error_reporting(E_ALL)` and `ini_set('display_errors', true);` – Raymond Nijland Sep 24 '17 at 11:43
  • 1
    Seems like the submit-button isn't inside the form at all - or this isn't the actual code. – Qirel Sep 24 '17 at 11:47
  • @Qirel maybe it's triggerd by AJAX request.. i have voted to close the question because it's incomplete. – Raymond Nijland Sep 24 '17 at 11:51
  • If it was triggered by AJAX, I would assume the form had a class or an ID attribute. But indeed, it's not complete - and OP is seemingly M.I.A. too. – Qirel Sep 24 '17 at 11:52

2 Answers2

0

You are getting blank page, because, either of below lines of code is returning false

if(!empty($_POST['others_delete'])){

or

if(isset($_POST['submit'])){
Ravi
  • 30,829
  • 42
  • 119
  • 173
0

you check if(isset($_POST['submit'])) but your does not contain an input elemt with that name so your $_POST variable should not have that key resulting in your then-part being skipped.

you can check by calling var_dump($_POST);at the end or start of your php file.

as stated in the comments of your question: You are using mysqli. You should consider using prepared statements. They are a faster on single usage (personal observation) and a lot faster if reused like in your case. Another advantage: they work against sql injection!

chilly
  • 304
  • 2
  • 13