1

I'm trying to delete rows of MySQL date using Checkboxes. When I apply the script below it doesn't do what I want it to do. Action is not performed.. And I get

Undefined variable: checkbox in C:\wamp\www\project\topics.php on line 108

I tried echoing out the actual sql that is being sent for execution and it gave this

DELETE FROM forum_topics WHERE topic_id = intval()

PHP

<?php        

$topics = mysql_query(" SELECT topic_id , topic_head , 
                               topic_tags , topic_owner , topic_date
                        FROM   forum_topics ") or die (mysql_error());?>

         <form method='post' action='topics.php'>
             <div class='admin'><table>       
                        <tr>
                            <td >DELETE</td>
                            <td >ID</td>
                            <td>Title</td>
                            <td>Tags</td>
                            <td>Owner</td>
                            <td>Date</td>  
                        </tr>

                        <?php
        while($row=mysql_fetch_array($topics)){ ?>
                   <tr align=center> 
                     <td align="center" bgcolor="black">
<input name="checkbox[]" type="checkbox" value="<?php echo $row['topic_id'];?>">
                </td>
                 <td><?php echo intval($row['ID']); ?></td>
                 <td><?php echo htmlspecialchars($row['topic_head']); ?></td>
                 <td><?php echo htmlspecialchars($row['topic_tags']); ?></td>
                 <td>><?php echo htmlspecialchars($row['topic_owner']); ?></td>
               <td>
                            <?php           
                                  $date = date_create($row['topic_date']) ;
                                  echo  date_format($date, 'F j, Y, g:i A'); 
                            ?>
                        </td>
                        <?php echo"
                        </tr>"; 
                        }?> 

         <td ><input name="delete" type="submit" value="DELETE"></td>
        <?php
         // Check if delete button active, start this
        if (isset($_POST['delete'])) {
          for($i=0;$i<$count;$i++){ 
           $del_id = $checkbox[$i]; 
          $sql = "DELETE FROM forum_topics WHERE topic_id = intval($del_id)"; 
                  mysql_query($sql);
                 }   
               }
            ?>
                       </table>
                       </form>
                       </div>
4
  • 1
    Where do you specify $checkbox? Commented Sep 12, 2012 at 20:59
  • Yeah $checkbox appears out of no where Commented Sep 12, 2012 at 21:00
  • @ibu I have the checkbox in the <input> <input name="checkbox[]" type="checkbox" value="<?php echo $row['topic_id'];?>"> Commented Sep 12, 2012 at 21:09
  • 1
    You can develop your own solution by adapting my example on a similar question: stackoverflow.com/questions/11123782/… Commented Sep 12, 2012 at 21:21

3 Answers 3

3

Well you need to rewrite your deletion logic into something like this

if (isset($_POST['delete']) && isset($_POST['checkbox'])) {
    foreach($_POST['checkbox'] as $del_id){
        $del_id = (int)$del_id;
        $sql = "DELETE FROM forum_topics WHERE topic_id = $del_id"; 
        mysql_query($sql);
    }
    header('Location: topics.php');
}

Also consider to do not use mysql_* functions since deprecation process has begun.

EDIT Added missed parenthesis in if condition

EDIT Also I recommend to redirect user to the same page after form submission it'll prevent from repeated form submits on page refresh. See updated code. And of course deletion logic should be placed before you made any selects

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer, it worked. But I'm having to do the process twie, before it takes effect.
@DotOyes I guess you get all topics before deletion. Just put DELETE before your SELECT
2

where are you getting the value from the array ??

you should have something like

$array = $_POST[checkbox];

then u can do the for

foreach($array as $delID){

          $sql = "DELETE FROM forum_topics WHERE topic_id = $delID"; 
                  mysql_query($sql);

}

the validation for num values do it before everything

2 Comments

watch this: i change the value of the check box to '1) OR 1' and all your table data is deleted, SQL injection
yes, thats the reason i told him ... validation before everything.
1

Try replacing these lines:

 for($i=0;$i<$count;$i++){ 
    $del_id = $checkbox[$i]; 

for this ones

 $count=count($_POST['checkbox']);
 for($i=0;$i<$count;$i++){ 
    $del_id = $_POST['checkbox'][$i]; 

your $checkbox variable is supposed to work if php directive register_globals is on, but that directive has been for a long time been default to off besides it's been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0 because of security related issues.

3 Comments

Thanks for the answer, I get an Undefined Offset on that line when I replaced with your code.
where is defined $count ? I dont see it in your code, it should be right above the for($i=0;$i<$count;$i++){ line, and be like $count = count($_POST['checkbox']);
I've updated my answer with the $count definition, try that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.