无法使用复选框删除行

时间:2014-04-25 07:14:31

标签: php html checkbox

我正在尝试从使用复选框连接到数据库的表中删除一行(如果它只有一行或多行就没关系),但它无法正常工作,因为没有任何反应。它不会删除,只有刷新才会出现错误或警告。

PHP:

 <?php
    if(isset($_POST['del_event']))
    {
     if(isset($_POST['check']) && count($_POST['check']))
     {
      $array = array("check" => $_POST['check']);
      $id = implode(',', $array['check']);
      $result = mysql_query("DELETE FROM event WHERE event_id = '$id'") or die(mysql_error());
      }
    }
  ?>

html:

<form class="buttons" method="post" action="event.php">
<div class="button-wrap">
<input type="button" id="add_event" name="add_event" value="Add Event"/>
<input type="submit" id="del_event" name="del_event" value="Delete Event"/>
</div>  
 </form>
<tbody class="tbody-event-list-table2">
<?php
 require "connection.php";
  $check = mysql_query("SELECT * FROM event") or die(mysql_error());
 if(mysql_num_rows($check) > 0)
{
while($row = mysql_fetch_array($check))
{
     $id = $row['event_id'];
    $name = $row['event_name'];
    $start = $row['start'];
    $end = $row['end'];
    $venue = $row['event_venue'];
echo 
"<tr>
  <td><input type='checkbox' name='check' id='check' class='check' value='$id'/><a href=''        
 class='event-link'>$name</a></td><td>$start</td><td>$end</td><td>$venue</td>";
 echo "</tr>";
 }
}   
else
{
  echo 
  "<tr>
   <td colspan='4'>There Are No Events.</td>
  </tr>";
}
?>
</tbody>

它是if(isset($_POST['del_event'])),因为'del_event'是删除按钮的名称,除非选中复选框,否则该按钮被禁用。

<script>
 var chkbox = $(".check"),
            button = $("#del_event");
       button.attr("disabled","disabled");
        chkbox.change(function(){
            if(this.checked){
                button.removeAttr("disabled");
            }else{
                button.attr("disabled","disabled");
            }
        });
  </script>

php版本是5.5.9

2 个答案:

答案 0 :(得分:0)

如果它是检查变量

,这仍然会给你一个未定义的错误
if (isset($_POST['check']) && count($_POST['check']) != 0)

但如果表单中的复选框处于关闭状态,则不会随表单一起发送,因此它不存在。 Carpetsmoker是正确的一半,一个适当的解决方案是:

if (isset($_POST['check'])) {
    if ( (int) $_POST['check'] !== 0) {

    }
}
顺便说一下,最好总是将变量转换为您要检查的属性类型。 在PHP中,0可以是0,false或null。

使用===

进行安全类型检查以防止这种情况发生

答案 1 :(得分:0)

所以我修复了概率。首先我必须删除分离按钮和表格的“形式”,所以现在“形式”包裹着按钮和表格。第二是删除的php编码也发生了变化。

<form class="buttons" method="post" action="event.php">
<div class="button-wrap">
<input type="button" id="add_event" name="add_event" value="Add Event"/>
<input type="submit" id="del_event" name="del_event" value="Delete Event"/>
<input type="submit" id="edit_event" name="edit_event" value="Edit Event">
</div>
<div class="event-list-table2" >
    <table class="event-table">
            <thead>
                <tr>
                    <th>Event Name</th>
                    <th>Start Event</th>
                    <th>End Event</th>
                    <th>Venue</th>
                </tr>
            </thead>
            <tbody class="tbody-event-list-table2">
                <?php
                    require "connection.php";
                    $check = mysql_query("SELECT * FROM event") or die(mysql_error());
                    if(mysql_num_rows($check) > 0)
                    {
                        while($row = mysql_fetch_array($check))
                        {
                            $id = $row['event_id'];
                            $name = $row['event_name'];
                            $start = $row['start'];
                            $end = $row['end'];
                            $venue = $row['event_venue'];
                            echo 
                            "<tr>
                                <td><input type='checkbox' name='check[]' class='check' value='$id'><a href='' class='event-link' value='$id' name='event-link'>$name</a></td><td>$start</td><td>$end</td><td>$venue</td>";
                            echo "</tr>";
                        }
                    }   
                    else
                        {
                            echo 
                            "<tr>
                                <td colspan='4'>There Are No Events.</td>
                            </tr>";
                        }
                ?>
            </tbody>
<?php
    if (isset($_POST['del_event']) && isset($_POST['check']))
    {
        foreach($_POST['check'] as $del_id)
        {
            $del_id = (int)$del_id;
            $sql = mysql_query("DELETE FROM event WHERE event_id = $del_id") or die(mysql_error());
                if($sql)
                {
             echo "<meta http-equiv=\"refresh\" content=\"0;URL=event.php\">";
}
        }
    }
?>
    </table>
</div>
     </form>