嘿,我通常通过这样做来获取ajax 发送的 js 对象文字:
$_POST['called']
$_POST['chk1']
etc etc...
但现在我有一个问题,我似乎无法找到解决方案。
根据选中的复选框数量,我循环(使用 js)查看所有复选框并将它们添加到最终看起来像这样的 js 对象:
doBulk = {
called: "Approved",
chk0: "1789156857",
chk2: "5134465673753",
chk3: "234123554646",
chk10: "25511545542"
};
现在chkXX可以是 0-19 之间的任何数字(因此每页 20 个复选框)。我正在将它发送到我的 PHP 页面,但我不确定如何进行循环以获取更新数据库所需的数据。
$chk1 = $_POST['chk0'];
$chk2 = $_POST['chk1'];
$chk3 = $_POST['chk2'];
$chk4 = $_POST['chk3'];
$chk5 = $_POST['chk4'];
$chk6 = $_POST['chk5'];
$chk7 = $_POST['chk6'];
$chk8 = $_POST['chk7'];
$chk9 = $_POST['chk8'];
$chk10 = $_POST['chk9'];
$chk11 = $_POST['chk10'];
$chk12 = $_POST['chk11'];
$chk13 = $_POST['chk12'];
$chk14 = $_POST['chk13'];
$chk15 = $_POST['chk14'];
$chk16 = $_POST['chk15'];
$chk17 = $_POST['chk16'];
$chk18 = $_POST['chk17'];
$chk19 = $_POST['chk18'];
$chk20 = $_POST['chk19'];
我可以做很多if来检查每个人是否有数据,但必须有更好的方法来做到这一点?
因此,如果我正在执行批量 mySQL 更新,那么我将不得不为每个具有上述值的复选框运行查询?是否还有更好的方法来一次更新所有需要的记录?
$result = mysql_query("UPDATE userAccount SET Accept = 1 WHERE ID = " . $chk1 . "");
谢谢!
更新
foreach($_POST as $key => $value)
{
// $key = CHK1-20
// $value = XXXXXXXXX
$dbBuilder = $value . ", " . $dbBuilder;
}
$dbBuilder = '(' . $dbBuilder . ')';
$result = mysql_query("UPDATE userAccount SET Accept = 1 WHERE ID in $dbBuilder");