PDA

View Full Version : Multiple Delete Error



Titan85
01-17-2007, 11:48 PM
Ok, I am attempting to make a script that will allow multiple deletion at the same time (via check boxes). I am not really sure how exactly to do it, but so far what I have picked up from a few tutorials is this:
<?php
require('config.php');

$result = mysql_query("SELECT * FROM `m_delete` ") or die (mysql_error());
$count = mysql_num_rows($result);

echo '<table width="400">';

while($row = mysql_fetch_array($result)) {
extract($row);

echo '
<tr>
<td>'.$id.'</td>
<td><input type="checkbox" name="checkbox[]" /></td>
<td>'.$name.'</td>
<td>'.$email.'</td>
</tr>
';
}

echo '</table>
<br />
<input type="submit" name="delete" id="delete" value="Delete Selected" />
';

if($delete) {
for($i=0;$i<=$count;$i++) {
$del_id = $checkbox[$i];
$sql = "DELETE FROM `m_delete` WHERE `id` = '$del_id'";
$result = mysql_query($sql) or die (mysql_error());
}

if($result) {
echo 'Rows deleted!
<br />
<a href="'.$_SERVER['REQUEST_URI'].'">Back</a>';
}
}

mysql_close();
?>The problem I am having is that when I hit the "delete selected" button, nothing happens. Also, I would like to know what exactly this code does/means:
for($i=0;$i<=$count;$i++) {I have seen it lots of times, but can't quite figure out what $i is for. I appreciate any help :)

Titan85
01-18-2007, 09:19 PM
Does anyone know what could be wrong, I find it hard to believe that no one has ever done something like this ;)

thetestingsite
01-19-2007, 03:28 PM
Assuming you have either register globals set to "On" or you assigned the variable earlier in the script, try changing the below:



for($i=0;$i<=$count;$i++) {
$del_id = $checkbox[$i];
$sql = "DELETE FROM `m_delete` WHERE `id` = '$del_id'";
$result = mysql_query($sql) or die (mysql_error());
}


to this:



foreach ($checkbox as $del_id) {
$sql = "DELETE FROM `m_delete` WHERE `id` = '$del_id'";
$result = mysql_query($sql) or die (mysql_error());
}


Hope this helps.

Titan85
01-20-2007, 03:00 PM
Assuming you have either register globals set to "On" or you assigned the variable earlier in the script, try changing the below:



for($i=0;$i<=$count;$i++) {
$del_id = $checkbox[$i];
$sql = "DELETE FROM `m_delete` WHERE `id` = '$del_id'";
$result = mysql_query($sql) or die (mysql_error());
}


to this:



foreach ($checkbox as $del_id) {
$sql = "DELETE FROM `m_delete` WHERE `id` = '$del_id'";
$result = mysql_query($sql) or die (mysql_error());
}


Hope this helps.I did that and when I hit delete, I get the error:
Warning: Invalid argument supplied for foreach() in /home/bntqann/public_html/testing/login/admin/index.php on line 112. Line 112 is the foreach statement you told me to use. Any idea whats wrong?

thetestingsite
01-20-2007, 05:00 PM
Assuming you have either register globals set to "On" or you assigned the variable earlier in the script

Did you check any of these things? If Register Globals is set to On, then the script should work. However; according to the error message you get, it seems that the variable $checkbox is not defined. So maybe even try changing (in the foreach loop) $checkbox to $_REQUEST['checkbox'] and see if that fixes it.

Hope this helps.

thetestingsite
01-20-2007, 05:23 PM
Something else I forgot to add is that you need to have a value for checkbox in your form. For example, where you have this:



<input type="checkbox" name="checkbox[]" />


should be like this:



<input type="checkbox" name="checkbox[]" value="'.$id.'" />


Sorry, didn't notice that before.