Jump to content

[SOLVED] Any idea why this isn't working? (Checkbox to delete data)


tfburges

Recommended Posts

On the preceding page, the checkboxes are named after the id number.

i.e.

<input type=checkbox name=1 value=1>
<input type=checkbox name=2 value=1>
etc.

 

The following code SHOULD look to see which rows were checked; and if they were, the rows are deleted.

$qid = "SELECT `id` FROM reports";
if ( ($rid = mysql_query( $qid, $link )) === FALSE )
exit( 'ID Query failed' );

while ( $did = mysql_fetch_assoc( $rid ) ) {
$cid = (isset($_POST['$did']) && $_POST['$did'] == '1')? 1 : 0;
//a checked box yields a 1; an unchecked box yields a 0
  if ($cid == '1') {
   mysql_query("DELETE FROM `reports` WHERE `id` = '$did'") or die ('MYSQL error: ' . mysql_error());
  }
}

If I was you I would echo the value in $cid while going through the loop. This way you'll be able to work out if the value is being set in the checkbox and this will help you debug your problem.

 

Another thing, if you insist on error checking a query like this it is generally done using the die() function.

 

<?php

$rs = mysql_query($sql) OR die("Query failed: ".mysql_error());

?>

$did = mysql_fetch_assoc( $rid )

this code returns an array you need to go

 while ( $row = mysql_fetch_assoc( $rid ) ) {
$did=$row['id'];
$cid = (isset($_POST['$did']) && $_POST['$did'] == '1')? 1 : 0;
//a checked box yields a 1; an unchecked box yields a 0
  if ($cid == '1') {
   mysql_query("DELETE FROM `reports` WHERE `id` = '$did'") or die ('MYSQL error: ' . mysql_error());
  }

 

Scott.

yes there is a better way. Create an array from the checkboxes then you can run a loop to delete the entries

 

form page. make the name an array and the value of the checkbox will be the id of the row

<input type=checkbox name=del[] value=$id>
<input type=checkbox name=del[] value=$id>

 

then on the page that will delete the rows

foreach($_POST['del'] as $del_id){
$sql = "DELETE FROM `reports` WHERE `id` = '$del_id' LIMIT 1";
$res = mysql_query($sql);
  if(!$res){
  echo "Could not delete row with id $del_id.<br>Error: ".mysql_error();
  } else {
  echo "Row deleted<br>";
  }
}

 

Ray

Sadly, that doesn't work with the way I have the page set up.  The data is split into sections and isn't necessarily in order, so the ID's don't match up.  I'm sure there's a way to get it to match properly though.

 

My first thought is just to change del[] to del[",row['id'],"].  Any objections?

 

I'm gonna try this and a few things later on and post the results.  In the mean time, I'm busy with something else.   :-X

Thanks a bunch!  That works...

 

...but I did have to do this:

My first thought is just to change del[] to del[",row['id'],"].  Any objections?

 

Overall:

while ( etc... ) {
<input type=checkbox name=del[",$datam['id'],"] value=",$datam['id'],">
}
...
while ( etc... ) {
<input type=checkbox name=del[",$datap['id'],"] value=",$datap['id'],">
}
...
while ( etc... ) {
<input type=checkbox name=del[",$datap['id'],"] value=",$datap['id'],">
}

etc...

$idlist = join(',' , $_POST['del']);
mysql_query("DELETE FROM `formz` WHERE `id` IN ($idlist)");

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.