Kentje
Kentje

Reputation: 37

Deleting Checked box data in Mysql Table from form

I am working on an inventory program and I need help on the following: I have a table that works through selecting data from my Database and showing this in a table. I now need to make it possible for a user to be able to delete bad/faulty records from the form ( Ofcourse u don't want to everytime have to go in the database itself to remove a record, would be stupid)

My code:

<div id="artikel-container">    
   <table class="table 1">
      <thead>
         <title>Inventory Grid.html</title>
         <meta charset = "UTF-8" />
         <style type = "text/css">
             table, td, th {
                 border: 1px solid black;
             } 
         </style>
      </thead>

      <tbody>
          <?php
             $con = mysqli_connect("localhost", "root", "admin", "inventarisdb");
             // Check connection
             if ( mysqli_connect_errno() ) {
                 echo "Failed to connect to MySQL: " . mysqli_connect_error();
             }

             $result = mysqli_query( $con, "SELECT * FROM BCD" );

             echo "<table border='0'>
                      <tr>
                         <th>Categorie</th>
                         <th>SerieNummer</th>
                         <th>MacAdress</th>
                         <th>ProductCode</th>
                         <th>Prijs</th>
                         <th>RekNummer</th>
                         <th>PaletNummer</th>
                         <th>Hoeveelheid</th>
                         <th>Aantekeningen/th>
                      </tr>";

               while( $row = mysqli_fetch_array( $result ) ) {
                   echo "<tr>";
                       echo "<td>" . $row['Categorie'] . "</td>";
                       echo "<td>" . $row['SerieNummer'] . "</td>";
                       echo "<td>" . $row['MacAdress'] . "</td>";
                       echo "<td>" . $row['ProductCode'] . "</td>";
                       echo "<td>" . $row['Prijs'] . "</td>";
                       echo "<td>" . $row['RekNummer'] . "</td>";
                       echo "<td>" . $row['PaletNummer'] . "</td>";
                       echo "<td>" . $row['Hoeveelheid'] . "</td>";
                       echo "<td>" . $row['Aantekeningen'] . "</td>";
                   echo "</tr>";
               }
          echo "</table>";
      mysqli_close($con);
    ?>

    </article>
</fieldset>

</tbody>

This code works perfectly!

What i want to achieve is to select Records from this table with a checkbos and delete them by clicking on a delete button in the bottom of the page programmed like this:

<Input type="submit" id="submit" name="submit" value="Verwijderen" />

Or at least with this principle!

(This is the second ever application I wrote, 1st was a webpage, and I have no idea how to research this. I keep finding questions and posts that are just not what i need :S Hope U can help me )

Upvotes: 0

Views: 1164

Answers (2)

Gatis Negribs
Gatis Negribs

Reputation: 56

I suggest you to use jQuery ajax so you will be able to delete entry one by one without page reload.

At first include jQuery library in HTML HEADER and the Javascript script file

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="script.js"></script>

script.js:

$(document).ready(function() {
  $(".delete").click(function(event) {
        var entryid = $(this).closest('tr').find('.entryid').val();
        alert('This entry will be deleted: ' + entryid); // this will alert you the value what you get. After finisgin to get correct value you can delete this
        $.ajax({
            type: "GET",
            url: "delete.php",
            data: "id=" + entryid
        });
        $(this).closest('tr').remove();
    });
});

in MySQL database must be uniqe ID for every entry named 'id'

Grid.html:

 echo "<td>" . $row['Aantekeningen'] . "</td>";
 echo '<td><input type="hidden" class="entryid" name="id" value='.$row['id'].' /><a href="#" class="delete">delete</a></td>';

delete.php:

// connect to database
$id = $_GET['id'];
$sql="DELETE FROM BCD WHERE id='$id'";

Upvotes: 1

user2417483
user2417483

Reputation:

First of all you need to wrap the table into a form

<div id="artikel-container">
<form name="form1" method="post" action="...">
<table class="table 1">
...
echo "</table></form>";
mysqli_close($con);

Next you need the checkboxs on the form $row['ID'] is the primary key of the table

  echo "<td>" . $row['Aantekeningen'] . "</td>";
  echo "<td><input type='checkbox' name='del[]' value='".$row['ID'] . "></td>";
  echo "</tr>";

In the php file called in the form's action attribute:

<?php
if( isset( $_POST['del'] ) {
  $del = $_POST['del'];
  for(x=0; x<count(del); x++ ) {
     $sql = 'DELETE FROM BCD WHERE ID='.$del[$x];
     //---execuste the query
  }
}
?>

Upvotes: 0

Related Questions