php - CASE 语句未运行两个 WHEN

标签 php mysql sql case

我遇到查询问题。 (我在帖子底部详细介绍)我有一张复选框表。我在 POST 上将复选框发送到 PHP 文件以更新 db。我可以确定是否有一个先前已选中的框在 POST 之前未选中的唯一方法是查询表中应已选中的内容以及是否未选中没有通过 POST 发送,那么它就未被检查。我决定在一个查询中处理所有这些。但出于某种原因,它不处理未经检查的情况。如果我选中之前未选中的一项,那么它将保持选中状态(它将在 db 中将其标记为选中)但是,如果我取消选中之前选中的一项,它将保持选中状态已选中(我希望它取消选中它)。

这是我创建复选框的代码:

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

这是我提交到的 PHP 文件的一部分:

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 列表放入名为 $checkins 的数组中,并且还获得了 check*< em>ed*ins 是在 POST 到名为 $checkedins 的数组之前检查的所有内容。如果它们不在 $checkins 列表中,那么我将其添加到 $checkins 数组中。因此 $checkins 现在包含所有要检查和未检查的行。我的查询应该做的是将数组中的所有内容更改为 0 的 1,并将 0 的所有内容更改为 1。它正在执行后者。我正在为另一半寻求帮助。

最佳答案

如果 checked_in 保证为零或一,您可以使用这个小技巧来完全避免`CASE:

SET checked_in = 1 - checked_in
WHERE ...

x0 时,表达式 1-x 计算结果为 1,当 x0 时,计算结果为 0x1 时。

关于php - CASE 语句未运行两个 WHEN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22283470/

相关文章:

php - javascript 对象的 XMLHttpRequest setRequestHeader 方法不起作用

PHP 部分相同的命名空间冲突

php - 使用 PHP 将 RSS 提要写入 MySQL

mysql - 特定顺序下的匹配表

php - Mysql 出现套接字故障

mysql - iReport 说 'The document has no pages'

php - 如何从 php 表单中的单选按钮获取值并检查另一个 php 中的值

php- 无法从 mysql (mysqli) 获取结果

sql - 插入值语句只能包含 SQL 数据仓库中的常量文字值或变量引用

java - 如何使用 MySQL 和 Hibernate 设置 group_concat_max_len