View Full Version : While $i is smaller than var count, do.

01-19-2010, 12:23 PM
// Delete Selected Files //
$values = $_POST['value'];
$i = 0;

//Select checked items
if (isset($_POST['DelSel'])) {
$count = count($values);
echo "Deleted records: ";
while ($i < $count) {
echo "$values[$i], ";
//Delete record
$delete = "DELETE FROM uploaded_files WHERE ID='$values[$i]'";
$result = mysql_query ($delete);
if (!$result)
die('Error: ' .mysql_error());
} //End While

$_POST[‘value’] is a checkbox which has the value of the primary key in the database. So for an example, it would be 201.

When the DelSel submit button is clicked the selected values are counted, whilst the number of values are higher then $i (0), records should be deleted for each value.

But for some reason if I only select one checkbox, nothing is deleted. And if I select multiple checkboxes at least one row is left.

01-19-2010, 12:29 PM
..and the question/conclusion?

01-19-2010, 12:39 PM
Sorry, I am sure I typed that part, instead of typing it up again I will edit my post with an email I just sent to a friend.

01-19-2010, 12:53 PM
echo "$values[$i], ";
//Delete record
$delete = "DELETE FROM uploaded_files WHERE ID='$values[$i]'"; Hope you don't get anything echoed for the highlighted line, above.
$delete = "DELETE FROM uploaded_files WHERE ID={$values[$i]}";

I'd do something like

if(isset($_POST['DelSel']) && is_array($_POST['DelSel'])){
foreach($_POST['DelSel'] as $value){
$sql="DELETE FROM uploaded_files WHERE ID in (".implode(',',$del). ")";
mysql_query ($sql) or die(mysql_error());