简体   繁体   English

如何使用复选框删除数据库结果行

[英]How to delete rows of database results using checkbox

I'm trying to delete rows of MySQL date using Checkboxes. 我正在尝试使用Checkboxes删除MySQL日期行。 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 我试图回显发送给执行的实际SQL,它给出了

DELETE FROM forum_topics WHERE topic_id = intval()

PHP 的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. 另外,由于弃用过程已经开始,因此请考虑不要使用mysql_*函数。

EDIT Added missed parenthesis in if condition 编辑新增错过括号中if条件

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 num值的验证先于一切

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. 如果启用了php指令register_globals,$checkbox变量应该可以工作,但是由于安全性原因,该指令自PHP 5.3.0起已被弃用,而PHP 5.4.0以来已被取消,该指令已经被默认禁用了。问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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