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());
  }
}

Link to comment
Share on other sites

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());

?>

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)");

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.