Log in

View Full Version : checklist to remove from db



baconDelta
01-27-2012, 10:56 PM
so i'm in the process of making a cms for some mods to be able to remove certain rows from a db.
the list of rows displays correctly, and i got the checkbox idea for each row from this thread,
http://www.dynamicdrive.com/forums/showthread.php?t=43923

and changed everything appropriately(or so i think) but when the delete button is pushed absolutely nothing happens.

here's what the list looks like:


<?php
$query = "SELECT *
FROM store
ORDER BY TIME DESC";
$result = mysql_query($query, $connection);
if (!$result) {
die("Database query failed: " . mysql_error());
}
echo '<br /><b><u>LATEST UPLOADS:</u></b><br /><br />';
echo '<table border=1 align=center cellpadding=5>';
echo '<tr><th>URL OF UPLOAD</th><th>FROM IP</th><th>TIME</th>';
echo '<th><input name="delete" type="submit" id="delete" value="Delete" method="POST"></th></tr>';
while ($row = mysql_fetch_array($result))
{
$link = "../".$row["url"];
echo "<tr>";
echo "<td><a href='{$link}'>{$row["url"]}</a></td>";
echo "<td>{$row["IP"]}</td>";
echo "<td>{$row["TIME"]}</td>";
echo "<td><input type=\"checkbox\" name=\"checkbox[]\" id=\"checkbox[]\" value=\"{$row["id"]}\" /></td>";
echo '</tr>';
}
if(isset($_POST['delete']))
{
if (count($_POST['checkbox']) > 0) {
foreach ($_POST['checkbox'] as $del_id) {
$sql = "DELETE FROM store WHERE id=' " . $del_id . " ' ";
$result = mysql_query($sql) or die(mysql_error());
if(mysql_affected_rows($result) > 0) echo 'Selected data rows Deleted';
}
}
}
?>


my code is slightly different than the link above in that i'm echoing my table in php.

i see that he is making a $count variable at the top but it doesn't look like it's used in deleting so i don't think i need this. i may be completely wrong lol

but anyway nothing happens on pressing the delete button, any ideas why?

fastsol1
01-28-2012, 02:33 AM
You don't have any open and closing form tags. You can't use the method=POST in the submit button tag. It doesn't do anything cause the page doesn't think you submitted the form cause there isn't any "form" on the page.

baconDelta
01-28-2012, 05:13 PM
oh wow good call. i put form tags around the entire thing, now the page refreshes when the button is pushed but nothing is deleted, whether i have some checked or not. hrmmm. something must be wrong with the delete function...

fastsol1
01-28-2012, 06:32 PM
Post your current code.

baconDelta
01-28-2012, 10:03 PM
<form>
<?php
$query = "SELECT *
FROM store
ORDER BY TIME DESC";
$result = mysql_query($query, $connection);
if (!$result) {
die("Database query failed: " . mysql_error());
}
echo '<br /><b><u>LATEST UPLOADS:</u></b><br /><br />';
echo '<table border=1 align=center cellpadding=5>';
echo '<tr><th>URL OF UPLOAD</th><th>FROM IP</th><th>TIME</th>';
echo '<th><input name="delete" type="submit" id="delete" value="Delete" method="POST"></th></tr>';
while ($row = mysql_fetch_array($result))
{
$link = "../".$row["url"];
echo "<tr>";
echo "<td><a href='{$link}'>{$row["url"]}</a></td>";
echo "<td>{$row["IP"]}</td>";
echo "<td>{$row["TIME"]}</td>";
echo "<td><input type=\"checkbox\" name=\"checkbox[]\" id=\"checkbox[]\" value=\"{$row["id"]}\" /></td>";
echo '</tr>';
}
if(isset($_POST['delete']))
{
if (count($_POST['checkbox']) > 0) {
foreach ($_POST['checkbox'] as $del_id) {
$sql = "DELETE FROM store WHERE id=' " . $del_id . " ' ";
$result = mysql_query($sql) or die(mysql_error());
if(mysql_affected_rows($result) > 0) echo 'Selected data rows Deleted';
}
}
}
?>
</form>

i've gotta fiddle with the last chunk of code, somethings not right there since nothings being deleted.

fastsol1
01-28-2012, 10:09 PM
Have you never used a form before? Take the method=POST out of the submit button. The top form tag should be like this

<form action="" method="post">

traq
01-29-2012, 01:40 AM
before you go too much further with this, be advised that you should NEVER have a form, like the one you posted, that allows the user to arbitrarily delete things from your database. You need to design this so it passes the request through a process that validates everything.

(Adding that functionality on later, "once you get it working," doesn't work.)

baconDelta
01-29-2012, 04:11 AM
ah man i'm derping hard. i have i have, just being an idiot. that worked. a strange bug though. after clicking delete it appears as if nothing happens until i refresh or visit the page again, then it shows that the entries have been deleted. it also doesn't echo the:
"selected data rows deleted" that's supposed to come up.

traq i should create a validation process even when it's just checkboxes for deleting entries? i can't imagine how there could be any harm but i'll take your word for it. but now i'm thinking about it and wondering what is there to validate? like to check in case it has already been deleted or something?

gadgetguy
01-29-2012, 04:30 AM
Perhaps it's something as simple as an undeclared $connection.

mysql_query($sql)

as compared to

mysql_query($query, $connection)

Hope that helps.

baconDelta
01-29-2012, 05:43 AM
gadgetguy changing
mysql_query($sql) to
mysql_query($query, $connection) just queries the db again instead of performing what's set to $sql. ($sql, $connection) isn't a bad idea for more appropriate code though, although i think the connection is assumed at that point since it works w/o it. but still good practice.

i'm also trying to use unlink on each selected row, since each row will have a url to a file. still figuring that part out hehe.