Log in

View Full Version : Multi Delete Problems



Titan85
08-30-2007, 11:26 PM
Well, I am using this code to do multiple deletes at one time:
// If delete was hit
else if ($_POST['delete']) {
if ($level >= 18) {
for ($i=0; $i<$chk; $i++) {
$del_id = $checkbox[$i];
$del = mysql_query("DELETE FROM `members` WHERE id = '$del_id'") or die ("Error Deleting Selected! \n<br />\n" .mysql_error());
}
echo('<meta http-equiv="refresh" content="2;URL='.$_SERVER['PHP_SELF'].'" /> <div class="message">The selected users have been deleted.</div>');
} else { echo('<meta http-equiv="refresh" content="2;URL='.$_SERVER['PHP_SELF'].'" /> <div class="error">You do not have permission to remove members</div>'); }
}For some reason, I just get the success message but nothing is deleted from the database. What can cause this?

Thanks

Twey
08-31-2007, 12:12 AM
No such ID?

Your HTML is poorly formed: you're using XHTML syntax. It may be that you have decided to drop support for IE and really are using XHTML, but I'll assume you've simply failed to understand the ramifications of using it, as so many people do, and are serving it as HTML. The <meta> element can only go in the <head>, but the <div> can only go in the <body>; and a <p> would be more semantically appropriate here.

Dropping out of PHP parsing mode makes for more efficient and more readable PHP, as well as neater HTML.

Looping here is needlessly inefficient: do it in a single query with the SQL IN operator.

I'm again going to be pessimistic and assume that you've forgotten to escape the input from the checkboxes. If you haven't, remove the array_map() call, or it'll muck things up.
else if (isset($_POST['delete'])) {
if ($level >= 18) {
$del = mysql_query(sprintf('DELETE FROM members WHERE id IN ('&#37;s')',
implode('\', \'', array_map('mysql_real_escape_string', $checkbox)))
)
or die(sprintf('<p>Query failed:</p><p>%s</p>', mysql_error()));
?>
<meta http-equiv="refresh" content="2;URL=<?php echo $_SERVER['PHP_SELF']; ?>">
</head>
<body>
<p class="message">The selected users have been deleted.</p>
<?php } else { ?>
<meta http-equiv="refresh" content="2;URL=<?php echo $_SERVER['PHP_SELF']; ?>">
</head>
<body>
<p class="error">You do not have permission to remove members</p>
<?php
}
}

Titan85
09-08-2007, 03:12 PM
Hey, sorry for the really delayed response, been tied up with work. I have been looking into the no ID problem and can't seem to find the problem. Here is the pages full code:
<?php

$level = $_SESSION['level'];

########## Pagination Code ##########
if ($_GET['page'] && is_numeric($_GET['page'])) { $page = $_GET['page']; } else { $page = 1; }

$cur = ($page * $maxAdmin) - $maxAdmin;
$get = mysql_query("SELECT * FROM `links`") or die ("Error Getting Data! \n<br />\n" .mysql_error());
$total = mysql_num_rows($get);
$totalPages = ($total / $maxAdmin);

if ($page > 1) { $prev = ($page - 1); $prevLink = '<a href="?page='.$prev.'">&lt;&lt;Previous</a>'; }
if ($page < $totalPages) { $next = ($page + 1); $nextLink = '<a href="?page='.$next.'">Next&gt;&gt;</a>'; }

for ($i=1; $i < $totalPages + 1; $i++) {
if ($page == $i) { $nav .= "<b>$i</b> "; } else { $nav .= '<a href="?page='.$i.'">'.$i.'</a> '; }
}
########## /Pagination Code ##########


########## If No Action ##########
if (!$_GET['act'] && !$_POST['delete']) {
if ($level < 18) { die('<meta http-equiv="refresh" content="2;URL=index.php" /> <div class="error">You do not have permission to access this page</div>'); } else {
$get = mysql_query("SELECT * FROM `links` ORDER BY id DESC LIMIT $cur, $maxAdmin") or die ("Error Getting Users! \n<br />\n" .mysql_error());
$u = mysql_fetch_array($get);
$chk = mysql_num_rows($get);

// If No Resuts
if ($chk < 1) { echo('No links at this time.'); } else { $count = 1; $color = $col1; ?>

<b>Navigation:</b> <a href="add.php">Add New Link</a> <br /><br />
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<table width="500" align="center" class="admin_table">
<tr bgcolor="<?php echo $col3 ?>">
<td>ID</td>
<td>Title</td>
<td>URL</td>
<td>View Level</td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php while ($u = mysql_fetch_array($get)) { $l = parse_url($u['url']); ?>
<tr bgcolor="<?php echo $color ?>">
<td><?php echo $u['id'] ?></td>
<td><?php echo $u['title'] ?></a></td>
<td><a href="<?php echo $u['url'] ?>" target="_blank"><?php echo $l[path] ?></a></td>
<td><?php echo $u['viewLvl'] ?></td>
<td><a href="?act=edit&amp;id=<?php echo $u['id'] ?>"><img src="../../images/edit.png" alt="Edit" border="0" /></a></td>
<td><input type="checkbox" name="checkbox[]" value="<?php echo $u['id'] ?>" /></td>
</tr>
<?php ++$count; if ($color == $col1) { $color = $col2; } else {$color = $col1; }
} ?>
</table>
<div align="center"><input type="submit" name="delete" value="Delete Selected" /></div>
</form>

<?php echo("<div align=\"center\">Pages: $prevLink $nav $nextLink</div>");
echo '
<br /><br />
<div align="center"><a href="javascript:history.back();">&lt;&lt;Back</a></div>';
}
}
}


########## If Action Was Set ##########

// If Edit Was Hit
if ($_GET['act'] == 'edit') { require('edit.inc.php'); }
//

// If delete was hit
else if ($_POST['delete']) {
for ($z=0; $z<$chk; $z++) {
$del_id = $checkbox[$z];
$del = mysql_query("DELETE FROM `links` WHERE id = '$del_id'") or die ("Error Deleting Selected! \n<br />\n" .mysql_error());
}
echo('<meta http-equiv="refresh" content="2;URL='.$_SERVER['PHP_SELF'].'" /> <div class="message">The selected links have been deleted.</div>');
}
//

?>Is there any obvious reason that it may not be getting the proper IDs to delete the lines from the database? Thanks