如果取消选中我的SQL表中的复选框删除行

Actually I am very new to php.. I have a task that select records from database as checkboxes checked. When I uncheck a checkbox it should delete that particular record from my database..

My code is

<?php
   if(isset($_POST['delete']))
       {
        $test=$_POST['test'];

          $qun1=$_POST['question1'];
      //DB connection 
            $CON=mysql_connect("localhost","root","");
             mysql_select_db("Dbname");


         if($qun1!=0 && $test!=0)
           {
              foreach($qun1 as $qunestion)
             {
              echo $question;   //this is for testing 

               $query=mysql_query("delete from test_question where test_questions_id='$test' AND question_id  IN ('$question') " ) or die(mysql_error());
            }

         if($query)
         { 
             echo "success";
         }
       else
        {
         echo "No";

        }

   }
 }

?>

my code is working properly but if i use NOT IN in the place of IN it is not working..why?..if unchecked the records it should be delete..i already retrieve records from database as checked fields..

My html markup:

     <script> 
      function fun2(ts)
       {
         $.post("ajax2.php",{qs:ts},function(data) 
         { 
           document.getElementById('div1').innerHTML=data 
          }) 

       }  
     </script> 
    <form action="somepage.php" method="post"> 
     //dynamic selection test code 
    <select name="test" id="test" onChange="fun2(this.value);">     
    <option value="">Select Test</option> </select> 
      <div id="div1" > </div>
    <input type="submit" name="delete" value="Delete" >
  </from>

my ajax code:

      <?php

        $qs=$_REQUEST['qs'];

        mysql_connect("localhost","root","");
        mysql_select_db("advancedge");

       $sql=mysql_query("select question_id  from test_question where test_questions_id='$qs'");

    if($sql)
      {
       while($rec=mysql_fetch_row($sql))
       {
         echo "<input type='checkbox' name='question1[]' value='$rec[0]' checked='checked' >";



        }
    }
  ?>

If I understand correctly, you want to delete as soon as the item is unchecked? To check if a checkbox is unchecked, you have to use javascript. PHP runs on the server, not the client side (you could use ajax, but I would recommend not to for this).

I would recommend, you delete whatever you want to on form submission, if you don't know how to do that,I can tell you if you post the html part up.

I would also recommend you learn PHP from a course on the internet, so you know some standards and get some good practices. (Youtube or Lynda.com)

It is an old post, but if someone end up here - this might solve the problem:

<?php
  echo "<form action='' method='post'>";

  echo "<input type='checkbox' id='chk_1' name='chk_1' value='First' />chk 1 </br>";
  echo "<input type='checkbox' id='chk_2' name='chk_2' value='Second' />chk 2 </br>";

  echo "<input type='submit' />";
  echo "</form>";

  $check = array();

  // Set all checkboxes to 0 if unchecked
  for($i = 1; $i<=2; $i++) {
      $check["chk_$i"] = isset($_POST["chk_$i"]) ? 1 : 0;
   }

  // Output of the array  
  echo '<pre>';
  var_export($check);         
  echo '</pre>';

The code will set all unchecked checkboxes to 0. Then you have to take care of that using something like this:

<?php
  if($_GET['chk_1'] == 0) {
    $sql = "DELETE FROM mytable WHERE...";
  }