Forum Moderators: coopster
The idea is that I can check any listings I want deleted, press the submit button, and delete the checked listings.
Everything looks ok but instead of deleting I get the error message I put in "Problem Deleting"
The code:
<?php
$linkID = @mysql_connect('localhost', '', '')
or die('Could not connect');
@mysql_select_db('breederlist') or die ('Could not get DB');
$query = "SELECT username, link, agree, name, last, organization, title, contact, email, phone, state,
availability, description, activities, iconName FROM listing ORDER BY state";
$result = mysql_query($query);
echo "<table id='table1'><tr><td class='icon'><h2>Location</h2></td>
<td class='contact'><h2>Contact</h2></td>
<td class='available'><h2>Puppies Available</h2></td>
<td class='activity'><h2>Activities</h2></td>
<td class='details'><h2>Details</h2></td></tr></table>
<form name='form1' id='form1' enctype='multipart/form-data'
action ='interface.php' method = 'post' style='float:left'>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$username = $row['username'];
$link = $row['link'];
$agree = $row['agree'];
$name = $row['name'];
$last = $row['last'];
$organization = $row['organization'];
$title = $row['title'];
$contact = $row['contact'];
$email = $row['email'];
$phone = $row['phone'];
$state = $row['state'];
$availability = $row['availability'];
$description = $row['description'];
$describe = str_replace("\r\n", "<br />", $description);
$describe = str_replace("\n", "<br />", $describe);
$describe = str_replace("\r", "<br />", $describe);
$activities = $row['activities'];
$iconName = $row['iconName'];
echo "<h3>" . $state . " - " . $organization .
"<input type='checkbox' name='" . $username . "' value ='" . $username . "' /></h3>
<table class='table2'>
<tr valign='top'>
<td class='icon'><img src='builders/make_listing_2/images/" . $iconName . "' / ></td>
<td class='contact'>" . $contact . "<br />
<a href='builders/make_listing_2/breeder_mail.php?address=" . $email . "
&title=" . $title . "' target='_blank'>Email</a><br />"
. $phone . "<br />" . $username .
"</td>
<td class='available'>". $availability . "</td>
<td class='activity'>" . str_replace("¦", "<br />", $activities) . "</td>
<td class='details'><h3>" . $title . "</h3><p>" . $describe . "</p></td>
</tr>
</table>";
}
echo "<input type='submit' name='submit' value='submit' /></form>";
if (isset($_POST['submit']))
{
$item = (isset($_POST[$username])? $_POST[$username] : '');
if ($item == $row['username'])
{
$query = "DELETE FROM listing WHERE username='$item'";
$result = mysql_query($query);
if ((mysql_affected_rows() == 0) ¦¦ mysql_affected_rows() == -1)
{
echo "Problem Deleting";
exit;
}
}
}
?>
[edited by: eelixduppy at 10:57 pm (utc) on Sep. 2, 2007]
[edit reason] removed specifics [/edit]
The way you've written that, though, it will only ever delete the very last name in the list. How about something like this:
if(isset($_POST['submit'])) {
unset($_POST['submit']);
foreach($_POST as $username) {
$query = "DELETE FROM listing WHERE username='$username'";
$result = mysql_query($query);
if ((mysql_affected_rows() == 0) ¦¦ (mysql_affected_rows() == -1)) {
echo "Problem Deleting with this: $query";
exit;
} // EndIf didn't affect any rows
} // EndForEach checked box
} // EndIf form submitted