Welcome to WebmasterWorld Guest from 54.162.19.123

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

An interface for deleting database rows.

Can anyone see my problem?

     
10:56 pm on Sep 2, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 4, 2006
posts:85
votes: 0


I have a listing page which outputs a series of listings. Now I'm trying to make an interface for myself to delete some of them. It is similar to the normal listing page but I've added a chackbox to each listing and a form submit.

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]

10:07 am on Sept 3, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 16, 2007
posts:914
votes: 0


Try echoing $query to see what you're trying to execute exactly.

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

5:49 pm on Sept 3, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 4, 2006
posts:85
votes: 0


Thanks cameraman,

After reviewing your code I realized that, as usual, I was making things more complicated than then needed to be.

I just foreached the chkbox array, which holds the usernames, and deleted each of those. It seems to work now.

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members