php html mysql在where条件下更新多个复选框

but5z9lq  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(303)

我想用多个where条件更新我的表,例如表名是zz

+----+------+------+
    | id | X1   | X2   |
    +----+------+------+
    |  1 |  xya | Abc  |
    |  2 |  yaz | xyz  | 
    |  3 |  wee | xsc  | 
    |  4 |  fss | xcs  |
    |  5 |   eer| XXX  |
    +----+------+------+

我的问题是这样的

UPDATE `zz` SET `x1` = 'hi'  WHERE `zz.`id` = 1 and zz.`id` = 2 and zz.`id` = 3

通过如下复选框选择“id”

<html>

<head>
 <script>
 function getcheckboxes() {
var node_list = document.getElementsByTagName('input');
var checkboxes = [];
for (var i = 0; i < node_list.length; i++) 
{
    var node = node_list[i];
    if (node.getAttribute('type') == 'checkbox') 
{
        checkboxes.push(node);
    }
} 
return checkboxes;
 }
  function toggle(source) {
   checkboxes = getcheckboxes();
   for(var i=0, n=checkboxes.length;i<n;i++) 
  {
checkboxes[i].checked = source.checked;
  }
 }
 </script>
</head>

  <body>
  <form action="#" method="post">
  <input type="checkbox" name="foo1" value="1"> 1<br/>
  <input type="checkbox" name="foo2" value="2"> 2<br/>
  <input type="checkbox" name="foo3" value="3"> 3<br/>
  <input type="checkbox" name="foo4" value="4"> 4<br/>

  <input type="checkbox" onClick="toggle(this)" /> Toggle All<br/>
   <input type="submit" name="submit" value="Submit"/>
   </form>
  </body>

  </html>

所以我想通过这个复选框选择一些id

wdebmtf2

wdebmtf21#

sql in运算符in运算符允许您在where子句中指定多个值。in运算符是多个或多个条件的简写。 UPDATE***WHERE column_name IN (value1, value2, ...); 你可以看看这个。在这里

puruo6ea

puruo6ea2#

添加以下jqueryajax代码

$("form").submit(function (e) {
                e.preventDefault();
                var arr=[];
                $('input:checkbox:checked').each(function () {
                    arr.push($(this).val());
                 });
                    $.ajax({
                        type: "POST",
                        url: "ajaxsubmit.php",
                        data: {data:arr},
                        success: function (result) {
                            alert(result);
                        }
                    });

                return false;
            });

在php端添加以下代码:

<?php

$updateWhere=$_POST['data'];
$where=array();
foreach($updateWhere as $value){
    $where[] = " `zz.`id`=".$value;
}
$sql="UPDATE `zz` SET `x1` = 'hi' "; 
$sql.=' WHERE '. implode(' AND ', $where);
echo $sql; die;

相关问题