我想用多个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
答案 0 :(得分:0)
SQL IN运算符 IN运算符允许您在WHERE子句中指定多个值。 IN运算符是多个OR条件的简写。
UPDATE ***
WHERE column_name IN (value1, value2, ...);
您可以检查一下。 Here
答案 1 :(得分:0)
添加以下jQuery ajax代码
$("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;