我有一个像这样的查询创建的表:
while ($row = mysqli_fetch_array($result))
{
$fields[] = $row['unit']; $fields[] = $row['reservation']; $fields[] = $row['guest'];
$fields[] = $row['arrival']; $fields[] = $row['departure']; $fields[] = $row['balance'];
$fields[] = $row['checked_in'];
//rowid == id of table (the row corresponds to the row in the table)
$rowid = $row['reservation'];
$count = count($fields);
//make checkboxes
echo "<tr class='edit_tr' id='".$rowid."'>";
echo "<td><input type='checkbox' value='".$fields[1]."' name='checkins[]'/></td>";
for ($j = 0; $j < $count; $j++)
{
echo "<td><span id='".$fields[1]."'>", ($j+1 == $count ? ($fields[$j] == 0 ? "Not Checked In" : "Checked In") : "".$fields[$j]."");
echo "</span></td>";
}
echo "</tr>";
//empty array
$fields = "";
}
我POST
将PHP文件的所有复选框UPDATE
发送到if(isset($_POST['checkins']))
{
//open connection
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
//create WHERE clause for new checkins
$where = " WHERE id =";
foreach ($_POST['checkins'] as $id) {
$where .= " $id OR id =";
}
//remove last 8 characters from WHERE clause
$where = substr($where, 0, -8);
echo $query = "UPDATE reservations
SET checked_in = CASE
WHEN checked_in = 0 THEN 1 END
$where)";
mysqli_query($con, $query);
mysqli_close($con);
}
数据库。
POST
我知道我不能checked_in
未经检查的框,但我需要一种方法来告诉PHP文件未选中的内容。这是我到目前为止所想到的:
1)查询所有checked_out
= 1的数据库,其中checked_out
= 0 (我只显示表中checked_in
= 0的行)如果id
不在POST
结果中,则设置{{1}} = 0。
2)将ajax数组发送到另一个PHP文件,其中包含以前检查过的所有框的列表,现在不是。
我相信还有更多,但这就是我的想法。不过,它们似乎都是变通办法。有人有更好的解决方案吗?