homepage Welcome to WebmasterWorld Guest from 23.20.220.79
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
An interface for deleting database rows.
Can anyone see my problem?
horseatingweeds




msg:3439563
 10:56 pm on Sep 2, 2007 (gmt 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]

 

cameraman




msg:3439861
 10:07 am on Sep 3, 2007 (gmt 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

horseatingweeds




msg:3440136
 5:49 pm on Sep 3, 2007 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved