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>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Dot Oyes
  • 163
  • 3
  • 12

3 Answers3

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

Alexander Larikov
  • 2,328
  • 15
  • 15
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

pl4g4
  • 101
  • 4
  • watch this: i change the value of the check box to `'1) OR 1'` and all your table data is deleted, [SQL injection](http://php.net/manual/en/security.database.sql-injection.php) – Ibu Sep 12 '12 at 21:33
  • yes, thats the reason i told him ... validation before everything. – pl4g4 Sep 13 '12 at 13:38
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.

Nelson
  • 49,283
  • 8
  • 68
  • 81
  • Thanks for the answer, I get an Undefined Offset on that line when I replaced with your code. – Dot Oyes Sep 12 '12 at 21:12
  • 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']);` – Nelson Sep 12 '12 at 21:22
  • I've **updated** my answer with the `$count` definition, try that. – Nelson Sep 12 '12 at 21:26