View Single Post
Old 05-23-2007, 10:19 AM   #1 (permalink)
Salchester
Salchester
 
Salchester's Avatar
 
Join Date: Jul 2005
Location: In a house
Posts: 230
Salchester is an unknown quantity at this point
Delete Records using an Array

Howdy Everyone,

I'm currently using the following, in order to delete records in a database upon clicking a delete button:

Code:
<?
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}
$view=$_GET["view"];
$id=$_GET["id"];
switch ($view) {
case "list":

mysql_select_db("WB", $con);

$result = mysql_query("SELECT * FROM Birds");

echo "<table border='1'>
<tr>
<th>Name</th>
<th>Species</th>
<th>Sex</th>
<th>Delete</th>
</tr>";

while($row = mysql_fetch_array($result))
{
  echo "<tr>";
  echo "<td>" . $row['Name'] . "</td>";
  echo "<td>" . $row['Species'] . "</td>";
  echo "<td>" . $row['Sex'] . "</td>";
  echo "<td><input type=\"button\" value=\"Delete\" onClick=\"parent.location='Select.php?view=delete&id=" . $row['ID'] . "'\"></td>";
  echo "</tr>";
}
echo "</table>";

    break;
case "delete":
    //insert your mysql string here deleting the record.
    mysql_select_db("WB", $con);

    mysql_query("DELETE FROM Birds WHERE id='$id'");

    mysql_close($con);
    //use the header function to get you back to the previous screen.
    $url="Select.php?view=list";
    header("Location: $url");
    break;
default:
    $url="Select.php?view=list";
    header("Location: $url");
} 
?>
Instead of using a static ID to identify a record, is there anyway of calculating the number of records in the table, and using the record number as the ID in order to delete a record.

Currently, I'm using a delete button next to each button, to delete the appropriate record, how can one delete button be used along with check boxes?

Many Thanks, much appreciated!!!
__________________
Many Thanks, in advance!

Salchester.
The Future Is Here - Are You Ready?
Salchester is offline   Reply With Quote