【问题标题】:Database update fails to work within php数据库更新无法在 php 中工作
【发布时间】:2018-06-18 12:42:57
【问题描述】:

我有一个相当简单的 php 页面,它在表格中显示来自数据库的结果列表,并且行的末尾是一个 Dismiss 按钮,我想单击它并让数据库更新以重置标志原始查询因此忽略了该消息。

我在这里查看了大量示例,并在按钮周围构建了一个表单来调用一个单独的 php 文件,该文件应该执行查询以更改数据库并返回原始页面,该页面将用更少的记录重绘。 一切正常,但没有发生数据库更新。

表格绘图

<table>
    <!-- lay out the table and populate the header row -->
    <tr>
        <th>Site</th>
        <th>Name</th>
        <th>Alarm</th>
        <th>Error</th>
        <th>Confirm</th>
    </tr>
    <?php
        $sql = "SELECT * FROM `hive_data` WHERE ack = 'y' "; //SQL query to find entries where the ack field is set 'y'
        $result = $conn->query($sql);
        while($row = mysqli_fetch_array($result)) { //start while loop to draw table one line per returned row
            $line = $row["row_id"]; //define page variables from table columns
            $site = $row["site"];
            $module = $row["module_id"];
            $alarm = $row["alarm"];
            $error = $row["error"];
            ?>
                <tr>
                    <!-- display each row returned listing the fields lists and a box to dismiss the alert -->
                    <td><?php echo $site; ?></td>
                    <td><?php echo $module; ?></td>
                    <td><?php echo $alarm; ?></td>
                    <td><?php echo $error; ?></td>
                    <td>
                        <form action = "data/dismiss_alerts.php" method="post">
                            <!-- last column of the table is a dismiss button -->
                            <input type="hidden" value="<?php echo $line ?>" name="line">       <!-- hidden input to send the row number to be changed -->
                            <input type="submit" value="Dismiss">                               <!-- submit button to post data to dismiss_alerts.php -->
                        </form>
                    </td>
                </tr>
            <?php
        } //close the while statement
    ?>
</table>

关闭提醒页面

if(isset($_POST['line'])) {
    $rowToUpdate = intval($_POST['line']);
    $sql = "UPDATE `hive_data` SET `ack` = 'n' WHERE row_id = " . $rowToUpdate . "";
    $result = mysqli_query($conn, $sql);
    header('Location: ../index.php?page=home'); // return to sending page.. 
}

执行会刷新原始页面,但数据库没有更新。

排除提示页面问题

if(isset($_POST['line'])) {
    $rowToUpdate = intval($_POST['line']);
    $sql = "UPDATE `hive_data` SET `ack` = 'n' WHERE row_id = " . $rowToUpdate . "";
    echo $sql;
    if($conn->connect_error) {
        echo "Connection failed";
    } else {
        echo "Connected";
        if($conn->query($sql) === TRUE) {
            echo "Record updated successfully";
        } else {
            echo "Error updating record: " . $conn->error;
        }
    }
}

第一个 if 子句运行良好,因此我留在了关闭警报页面上,并获得了查询的回声和一条消息,说明数据库已连接,但第二个 if 子句从不报告任何内容。

UPDATE `hive_data` SET `ack` = 'n' WHERE row_id = 253Connected

我已经在调用页面上测试了内联查询,因此它实际上会在每次刷新时删除表的最后一行,所以我很确定该位有效,但我真的很难过。

有什么想法吗?

【问题讨论】:

  • 一旦我的工作正常了,我会很乐意回去强化事情并设置错误陷阱,但现在这个网页只能由一个人访问:-)
  • @senile-sod 正如约翰之前提到的,您应该使用准备好的语句。它们可能比您意识到的要容易得多,并且比您当前进行查询的方式更安全。如果您想进行一些实验,我建议使用使准备好的语句更容易的众多数据库包装类之一,我个人可以推荐GrumpyPDO(免责声明),这是我不久前写的。
  • 是否显示任何成功或错误消息?

标签: php mysql


【解决方案1】:

好的,所以在在这里发布后的十分钟里花了一整个上午后,我发现尽管我的故障排除线在数据库上方没有连接,但我必须建立一个连接并断开连接到页面,它工作正常。

现在阅读准备好的语句并重新做一遍:-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 2015-07-03
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多