How to delete rows of database results using checkbox

杀马特。学长 韩版系。学妹 提交于 2019-12-11 10:18:53

问题


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>

回答1:


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




回答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




回答3:


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.



来源:https://stackoverflow.com/questions/12396096/how-to-delete-rows-of-database-results-using-checkbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!