PDA

View Full Version : Delete member from databse


bucket
08-24-2009, 11:41 PM
Hello I am working on a script to delete a member from an admin area, basicly a checkbox and a delete button so I can delete multiple at a time.

Here is the Php that shows all the members:
You will see a checkbox for delete.
<?php
require("inc/config.php");
$result = mysql_query("SELECT * FROM persons");
while($row = mysql_fetch_array($result))
{
echo "First Name: " . $row['FirstName'] . "";
echo "<br>";
echo "Last Name: " . $row['LastName'] . "";
echo "<br>";
echo "<a target=frame2 href='" ."profile.php?user1=". $row['FirstName'] ."'>Profile</a>";
echo "<br>";
echo "Date: " . $row['AddedDate'] . "";
echo "<br>";
echo "IP Address: " . $row['Ip'] . "";
echo "<br>";
echo '<input type="checkbox" name="delchk[]" value="'.$row['id'].'" />';
echo "<br>";
echo "***********************************************";
echo "<br>";
}
echo "</table>";
mysql_close($con);
?>

Here is the code that when u press delete:

if (is_array($_POST['delchk'])) {
foreach ($_POST['delchk'] as $delId) {
$query = "DELETE FROM persons WHERE person_id = $delId";
$result = mysql_query($query);
if (!$result) {
die("Error deleting persons! Query: $query<br />Error: ".mysql_error());
}
}
}



I currently need like a button that when clicked will delete all rows and somehow refresh the page.

bazz
08-24-2009, 11:49 PM
Read the forum rules. Do NOT cross-post. I have just read one where Fumigator and I replied. Indeed, my answer which preceded this thread, answers the question you have posed here.

bazz

bucket
08-25-2009, 12:44 AM
I changed the code a bit around:

if (is_array($_POST['delchk'])) {
for ($count = 0;count<count(delchk);count++)
{
$query = "DELETE FROM persons WHERE person_id = '$delchk[$count]'";
$result = mysql_query($query);
if (!$result) {
die("Error deleting persons! Query: $query<br />Error: ".mysql_error());
}
}
}