简体   繁体   中英

How to delete rows of database results using checkbox

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>

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM