Deleting MySQL data

The basic principle of deletion is a two-step process: first, identify the row or rows to be deleted; and second, remove the data with an SQL DELETE statement.As in an update, the first step requires a key value be provided, and any technique described for capturing keys in updates can be used. We assume here that a unique, primary key value for the row to be deleted is available.

Deleting rows using a primary key value is a minor modification to the update functionality of the script in Example 6-8. For example, the following fragment creates and runs a query to delete a specified customer identified by the value of $custID:

Deleting MySQL Rows

  // Connect to the database, clean, and validate data
  // We have a custID. Set up a delete query
  $query = "DELETE FROM customer
            WHERE cust_id = $custID";
  if ( (@ mysql_query ($query, $connection))
        && @ mysql_affected_rows(  ) == 1)
    // Query ran ok
    // Relocate to the receipt page with status=T
    header("Location: " .
           "delete_receipt.php?" .
           "cust_id=$custID" .
    // Failed to delete customer row.
    // Relocate to the status page with status=F
    header("Location: " .
           "delete_receipt.php?" .

by BrainBellupdated