使用MySQL和PHP删除页面上的项目

I am unsure of what path I should take for what I am wanting to do. The page loads some data from a mysql database with php. I made it so that a check box is generated with an incremented value starting wit 0 and so on for every value it finds in the database. What I am wanting to do is have that check box be so that when it is checked and they push the trashcan icon it will delete that row from the database. Where I am unsure is how to go about deleting it on button click and then reload the page. What would be the best way to do this? I am not needing the code or anything I just cant think of the best path to take to accomplish this.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Print Run</title>

<style type="text/css">
    .openRun{
        width:500px;    
    }
    .openRun th{
        text-align:center;
        background-color:#CCC;
    }
</style>
</head>
<body>
<div>
    <?php
        $totals = 0;
        $i = 0;
        $companyName = "";
        $quantity = "";
        $cardSize = "";
        $dateAdded = "";
        require("serverInfo.php");
        $printRun = mysql_query("SELECT * FROM `printRun` WHERE status = 'Open'");
        $row = mysql_fetch_array($printRun);
        echo 'Print Run #: ' . $row['Run'];
        $result = mysql_query("SELECT * FROM `printRun` WHERE status = 'Open'");

        while($row = mysql_fetch_array($result)){
            $companyName[$i] = $row['Company'];
            $quantity[$i] = $row['Quantity'];
            $cardSize[$i] = $row['Size'];
            $dateAdded[$i] = $row['Date'];
            $totals = intval($row['Quantity']) + $totals;
            $i++;
        }

        $arraySize = count($companyName);
        echo '<table class="openRun">';
        echo '<tr><th>Company</th><th>Quantity</th><th>Size</th><th>Date Added</th><th></th></tr>';
        for($i = 0; $i < $arraySize; $i++){     
            echo '<tr>';
            echo '<td>' . $companyName[$i] . "</td>";
            echo '<td>' . $quantity[$i] . "</td>";
            echo '<td>' . $cardSize[$i] . "</td>";
            echo '<td>' . $dateAdded[$i] . "</td>";
            echo '<td><input type="checkbox" name="box'. $i .'" value="'. $i .'" /></td>';
            echo '</tr>';
        }
        echo '<tr>';
        echo '<td style="font-weight:bold; border-style:solid; border-top-width:1px;">Totals</td>';
        echo '<td style="font-weight:bold; border-style:solid; border-top-width:1px;">' . $totals . "</td>";
        echo '<td></td>';
        echo '<td></td>';
        echo '<td><img src="images/trash.png" /></td>';
        echo '</tr>';
        echo '</table>';
        mysql_close($link);

    ?>
  <br />
  </div>


</body>
</html>

There's two ways of doing this. If you're not familiar with AJAX, or are scared about engaging with it, I'd suggest having a quick read of this tutorial just to get an overview.

With AJAX

If you don't want the page to reload, you're going to need to use AJAX to asynchronously send and receive data from the server. A typical example would be:

$('.trash_link').on('click', function() {
   $.ajax({
      url: 'path/to/script.php',
      data: 'variable='+$('.input_class').val(),
      dataType: 'html',
      success: function(response) {
         $('.container').html(response);
      }
   });
});

The crucial thing I'd advise is to put all of your layout logic in a controller or model function so that you don't repeat yourself. When data is sent to your server it can use this function to send back all your layout via the AJAX function. You can then insert the HTML inside your container element.

Without AJAX

All you do here is submit a form and your page reloads. For that reason you need to make sure you've already specified tags on your page. It really depends on what style you want your page to have - some people prefer reloads, some prefer the seamlessness of AJAX. Here is what you'd need if you chose to omit AJAX:

$('.trash_link').on('click', function() {
   $('form').submit();
});

You can also add a pop-up confirmation if you want to:

$('.trash_link').on('click', function() {
   if(confirm("Are you REALLY sure?") {
      $('form').submit();
   }
});

If you have an identifier in the table structure, you can just put

<input type='checkbox' name='ids[]' value='<?php echo $row['id']; ?>' />

Against every line of your table. Wrap the whole table in a form, make a submit button, in your script put something along these lines

if (isset($_POST['ids']) && is_array($_POST['ids'])) {
  // some input sanitizing required.
  $ids = array();
  foreach ($_POST['ids'] as $id) if (intval($id) > 0) $ids[] = intval($id);
  // $ids now hold the identifiers for the records to be deleted
  if (count($ids)) $query = mysql_query("DELETE FROM `printRun` WHERE id IN (" . implode(', ', $ids) . ")");
  // Then make a page refresh via HTTP 303 or otherwise to keep
  // to the POST-redirect-GET policy.
  header("HTTP/1.1 303 See Other");
  header("Location: http://whate.ver/your_page_is.php");
}

If you just want a trash can that when clicked on, reloads the page and deletes the item, then just have that image in a link that passes along the id or whatever unique identifier the row has.

<a href="/delete.php?id=<?= $row['id'] ?>">Delete</a>

Have it hit a new php file that handles pulling off the unique identifier and deleting the record, then forwards back to the listing page.

$recordToDelete = is_numeric($_GET['id']) ? $_GET['id'] : null;
if($recordToDelete != null) {
    $sql = "DELETE FROM `printRun` WHERE id = " . $recordToDelete;
    //execute sql
}
//redirect back to listing page
header('Location: /yourpage.php');