我遇到了查询问题。 (我在帖子底部详细介绍)我有一张复选框。我在POST时将复选框发送到PHP文件以更新db
。唯一可以判断在checked
之前是否存在未选中的POST
框,以查询表格中应该检查的内容以及是否未通过{{ 1}}然后它没有被检查。我决定在一个查询中处理所有这些。但是出于某些原因,它并没有处理未经检查的问题。如果我检查一个不优先检查,那么它将保持检查状态(它将在POST
中将其标记为已选中)但是,如果我取消选中之前已检查的一个,则会保持检查状态(I希望它取消选中它)。
以下是我创建复选框的代码:
db
以下是我提交的PHP文件的一部分:
echo "<table class='tables' border='1'>"; //start an HTML table
$fields =array();
$result = mysqli_query($con, "SELECT u.unit_nickname AS unit, r.id AS reservation,
CONCAT(g.fname,' ',g.lname) AS guest, r.arrival, r.departure,
r.total_price - COALESCE(p.payment_amt, 0) AS balance,
r.checked_in
FROM reservations r
JOIN units u ON r.unit = u.id
JOIN guests g ON r.guest = g.id LEFT JOIN
(SELECT reservation, SUM(payment_amt) payment_amt
FROM payments
GROUP BY reservation) AS p
ON r.id = p.reservation
WHERE r.checked_out = 0");
echo '<th></th><th>Unit</th><th>Reservation</th><th>Guest</th><th>Arrival</th>
<th>Depart</th><th>Balance</th><th>Status</th>';
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);
echo "<tr class='edit_tr' id='".$rowid."'>";
echo "<td><input type='checkbox' value='".$fields[1]."' name='checkins[]'";
if ($row['checked_in'] == 1)
echo "checked='yes'";
echo "/></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 = "";
}
echo "</table>"; //close the HTML table
我试图评论得很好,以解释发生了什么。
我唯一看到的问题是我的查询
if(isset($_POST['checkins']))
{
//open connection
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
//set POST to array
$checkins = array();
//get all checkins that are not checked out
$query = "SELECT id FROM reservations WHERE checked_in = 1 AND checked_out = 0";
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_assoc($result)) $checkedins[] = $row;
//create WHERE clause for new checkins
$where = " WHERE id =";
foreach ($_POST['checkins'] as $id) {
$where .= " $id OR id =";
array_push($checkins, $id);
}
//loop through $checkedins[]
foreach ($checkedins as $id) {
//if they were checked in before and are now not checked in, add to list
if (!in_array($id["id"], $checkins))
$where .= " ".$id['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
WHEN checked_in = 1 THEN 0 END
$where";
mysqli_query($con, $query);
mysqli_close($con);
}
我确信这与我的$query = "UPDATE reservations
SET checked_in = CASE
WHEN checked_in = 0 THEN 1
WHEN checked_in = 1 THEN 0 END
$where";
声明有关,但无论我多么修补它,我似乎都无法确定它。
应该发生的事情是:我将新CASE
的列表放入名为checkins
的数组中,并且我还获得了一个check * ed 的列表* ins是在POST之前 检查的所有那些名为$checkins
的数组。如果它们不在$checkedins
列表中,那么我将其添加到$checkins
数组中。因此$checkins
现在包含要检查和取消选中的所有行。我的查询应该做的是将数组中的所有内容更改为1为0,将所有内容从0更改为1.它正在执行后者。我正在寻求另一半的帮助。
答案 0 :(得分:1)
如果checked_in
保证为零或一,你可以使用这个小技巧来完全避免`CASE:
SET checked_in = 1 - checked_in
WHERE ...
当1-x
为1
时,表达式x
评估为0
,0
为x
时评估为1
。< / p>
答案 1 :(得分:0)
我决定做的是拆分查询。我创建了一个$checkin_where
字符串和一个$uncheck_where
字符串。我在每个中放置了相应的$id
s并使用了两个不同的UPDATE
查询:
//create WHERE clause for new checkins
$checkin_where = " WHERE id =";
$uncheck_where = " WHERE id =";
foreach ($_POST['checkins'] as $id) {
$checkin_where .= " $id OR id =";
array_push($checkins, $id);
}
//loop through $checkedins[]
foreach ($checkedins as $id) {
//if they were checked in before and are now not checked in, add to list
if (!in_array($id["id"], $checkins))
$uncheck_where .= " ".$id['id']." OR id =";
}
//remove last 8 characters from WHERE clause
$uncheck_where = substr($uncheck_where, 0, -8);
$checkin_where = substr($checkin_where, 0, -8);
if (strlen($checkin_where) > 5)
{
echo $query = "UPDATE reservations
SET checked_in = 1
$checkin_where";
mysqli_query($con, $query);
}
if (strlen($uncheck_where) > 5)
{
echo $query = "UPDATE reservations
SET checked_in = 0
$uncheck_where";
mysqli_query($con, $query);
}
mysqli_close($con);