dqvzfp6468 2013-07-25 10:09
浏览 41

如果取消选中我的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' >";



        }
    }
  ?>
  • 写回答

2条回答 默认 最新

  • dongqiao0953 2013-07-25 10:23
    关注

    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)

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line