Jump to content

need a php code to delete selected items of database items using checkbox


bindiya

Recommended Posts

You want to use several optional checkboxes at the same time...

 

If you don't want to do some huge javascript workaround, you will have to check for each one of those checkboxes... if it varies how many of them there is, then you specify that as a hidden input. I'm thinking:

 

$minchecks=0;
$maxchecks=50;
if(!empty($_POST['totalchecks'])){
  if($_POST['totalchecks']>$minchecks && $_POST['totalchecks']<$maxchecks){
    for($i=0;$i<$_POST['totalchecks'];$i++){
      if(!empty($_POST['check'.$i])){
        $checked[]=$_POST['check'.$i]; // this number can be faked, so make sure you check it
      }
    }
  }
}
print_r($checked);

echo '<form action="" method="post">
$i=0;
foreach($array AS $row){
  echo '<input type="checkbox" name="check'.$i.'" value="'.$row.'" />';
  $i++;
}
echo '<input type='hidden' name="totalchecks" value="'.$i.'" />
</form>

 

sorry if I did a typo or anything, and sure probably some better way of doing this... just an idea.

Link to comment
Share on other sites

Easier with arrays:

 

<input type="checkbox" name="checkbox[]" value="1"/>
<input type="checkbox" name="checkbox[]" value="2"/>
<input type="checkbox" name="checkbox[]" value="3"/>

 

$values = implode(',', array_map('mysql_real_escape_string', $_POST['checkboxes']));
$query = "DELETE FROM table_name WHERE id IN ($values)";

Link to comment
Share on other sites

Easier with arrays:

 

<input type="checkbox" name="checkbox[]" value="1"/>
<input type="checkbox" name="checkbox[]" value="2"/>
<input type="checkbox" name="checkbox[]" value="3"/>

 

$values = implode(',', array_map('mysql_real_escape_string', $_POST['checkboxes']));
$query = "DELETE FROM table_name WHERE id IN ($values)";

 

:thumb-up:  Definitely agree with that approach, but...

 

You have a typo. The field names are "checkbox[]", but the PHP code is referencing them as $_POST['checkboxes']. Besides, naming a field "checkbox" (or "checkboxes") is poor implementation. Always give elements/variables descriptive names.

 

Also, I am hyper-sensitive to preventing errors. The code above uses 'mysql_real_escape_string', but the values passed "should" be integers. If a non-integer is passed the query would fail. You could either do more intensive validating of the values, or just use intval() to ensure the values are interpreted as integers. Definitely several options you could go with here.

 

Updated/corrected:

<input type="checkbox" name="delete_ids[]" value="1"/>
<input type="checkbox" name="delete_ids[]" value="2"/>
<input type="checkbox" name="delete_ids[]" value="3"/>

 

$values = implode(',', array_map('intval', $_POST['delete_ids']));
$query = "DELETE FROM table_name WHERE id IN ($values)";

Link to comment
Share on other sites

mjdamato, I agree.  Normally, unless I'm actually posting code that is meant to be usable as is to solve a problem/error then my code is meant to be illustrative only.  Rather than bang out a bunch of code for someone to use, I normally just want to give them an example or something to push them in the right direction.

Link to comment
Share on other sites

thank u for the code.It works well ,if i check more than one checkboxes.But if i check for only one checkbox it gives me errors.

what will be the reason?

 

the error is

 

Warning: array_map() [function.array-map]: Argument #2 should be an array in /home/content/77/6911777/html/admin/themes/view_res.php on line 87

 

Warning: implode() [function.implode]: Invalid arguments passed in /home/content/77/6911777/html/admin/themes/view_res.php on line 87

 

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.