使用复选框删除多行

时间:2012-12-31 10:42:10

标签: php jquery mysql

我有以下jquery代码来删除多行表:

$(function(){
    $("a.delete").click(function(){
        page=$(this).attr("href");
        ids=new Array()
        a=0;
        $("input.chk:checked").each(function(){
            ids[a]=$(this).val();
            a++;
        })

        if(confirm("Are you sure want to delete?")){

                el=$(this)
                $.ajax({
                    url:page,
                    data:"id="+ids,
                    type:"GET",
                    success:function(res)
                    {
                        if(res==1)
                        {
                            $("input.chk:checked").each(function(){
                                $(this).parent().parent().remove();
                            })
                        }
                    }
                })
        }
        return false;
    })
})

复选框(名称为question_id的值正常,复选框位于while循环中):

<input name="chk[]" class="chk" type="checkbox" value="'<?php echo $row1['question_id'];?>'" />

删除链接(循环外):

<a href="delete_quiz_questions.php" class="delete">delete</a>

和删除记录的php页面:

<?php
   session_start();
   include('connect_db.php');

   $delete_id = $_GET['id'];

   $id = count($delete_id);

   if (count($id) > 0){
       foreach ($delete_id as $id_d){
           $sql = "DELETE FROM quiz_questions WHERE id_questions ='$id_d' AND id_quiz ='".$_SESSION['quiz_id']."'";
           $delete = mysql_query($sql);
       }
   }


?>

当我点击href按钮删除记录时显示此消息

Notice: Undefined index: id in C:\wamp\www\diplomatiki\delete_quiz_questions.php on line 5

Warning: Invalid argument supplied for foreach() in C:\wamp\www\diplomatiki\delete_quiz_questions.php on line 11

Notice: Undefined variable: delete in C:\wamp\www\diplomatiki\delete_quiz_questions.php on line 18
你可以帮帮我吗?

发送数据的php页面

$result=mysql_query("SELECT * FROM quiz_questions WHERE id_quiz='".$quiz_id."' ");

    if(mysql_num_rows($result)>0){
    $index = 1;

    while($row=mysql_fetch_array($result))
    {

    $result1=mysql_query("SELECT * FROM questions WHERE question_id ='".$row['id_questions']."'");

    if(mysql_num_rows($result1)>0){

    while($row1=mysql_fetch_array($result1))
    {

   $result2=mysql_query("SELECT * FROM question_".$row1['type']." WHERE id_question='".$row1['question_id']."' LIMIT 1");
   if(mysql_num_rows($result2)>0){
   $row2=mysql_fetch_array($result2);
    ?>
    <tr>
      <td><?php echo $index++; ?> </td>
      <td><input name="chk[]" class="chk" type="checkbox" value="'<?php echo $row1['question_id'];?>'" /></td>
      <td><?php echo $row1['type']; ?></td>
      <td><?php echo $row2['question']; ?></td>

    </tr>
    <?php  }}

    }


}
echo '<input type="hidden" name="quiz_id" id="quiz_id" style="text-align: center" value="' . $quiz_id . '" size=5/>';

?>

  </tbody>
</table>
<caption><a href="delete_quiz_questions.php" class="delete">delete</a></caption>

<?php } else echo 'sorry'; ?>

2 个答案:

答案 0 :(得分:1)

您必须使用explode将ID字符串转换为Array

$delete_id = explode(",", $_GET['id'])
$id = count($delete_id);
if (count($id) > 0)

最初你必须这样做。

if (isset($_GET['id'])) {

因此,您不会获得任何NoticeWarning

session_start();
include('connect_db.php');

if (isset($_GET['id'])) {
   $delete_id = explode(",", $_GET['id'])

   $id = count($delete_id);

   if (count($id) > 0)
   {
       foreach ($delete_id as $id_d)
       {
                $sql = "DELETE FROM quiz_questions WHERE id_questions ='$id_d' AND id_quiz ='".$_SESSION['quiz_id']."'";
                $delete = mysql_query($sql);
       }
   }
}

答案 1 :(得分:1)

首先,您必须检查会话quiz_id是否存在。如果存在,您应该检查此会话是否分配了任何值。