php - MySQL 表页未加载正确的数据

标签 php html mysql forms

我正在创建一个表,该表使用 PHP 从我拥有的 MySQL 数据库中提取数据。我认为我已经得到了我想要的一切,但是我遇到的唯一问题是结果似乎(由于缺乏更好的词)“落后”。我的意思是,我的第一页 index.php 是我接受用户对数据库编辑的地方。一旦他们点击Update,它就会将他们发送到我的results.php 文件,该文件应该实际执行 SQL UPDATE,然后显示更新后的表。

它根据 XAMPP 的数据库编辑器很好地更新了表。但是,当我说“后面”时,我的意思是页面加载、更新,但不会显示更新的数据,直到用户刷新页面或返回第一页然后返回。我不确定是什么原因造成的,所以我希望这里有人可以帮助我。我觉得原因很简单,因为我只是以错误的顺序运行代码,但我不确定。我的代码如下:

index.php

<html>
    <body>
        <?php
        include('dbconnect.php');
        $query = "SELECT * FROM vw_events";
        $result = mysqli_query($conn, $query);
        $count = mysqli_num_rows($result);
        ?>

        <form name="form1" method="post" action="results.php">
            <table width="auto" border="1" cellspacing="1" cellpadding="5">
                <tr>
                    <td align="center"><strong>Event ID</strong></td>
                    <td align="center"><strong>Title</strong></td>
                    <td align="center"><strong>Topic</strong></td>
                    <td align="center"><strong>Description</strong></td>
                    <td align="center"><strong>Event Date</strong></td>
                    <td align="center"><strong>Speaker</strong></td>
                    <td align="center"><strong>Building</strong></td>
                    <td align="center"><strong>Room</strong></td>
                </tr>

                <?php                    
                    while($rows=mysqli_fetch_array($result)) {
                ?>

                <tr>
                    <input name="event_id[]" type="hidden" id="event_id" value="<?php echo $rows['event_id']; ?>">
                    <td align="center">
                        <?php echo $rows['event_id'];?>
                    </td>
                    <td align="center">
                        <input name="title[]" type="text" id="title">
                    </td>
                    <td align="center">
                        <?php echo $rows['topic_name']; ?>
                    </td>
                    <td align="center">
                        <?php echo $rows['topic_description']; ?>
                    </td>
                    <td align="center">
                        <input name="date[]" type="date" id="date">
                    </td>
                    <td align="center">
                        <input title="Use reference tables below to enter speaker ID" name="speaker[]" type="text" id="speaker">
                    </td>

                    <td align="center">
                        <input title="Use reference tables below to enter building ID" name="building[]" type="text" id="building">
                    </td>


                    <td align="center">
                        <input title="Use reference tables below to enter Room ID" name="room[]" type="text" id="room">
                    </td>
                </tr>

                <?php
                    }
                ?>

                <tr>
                    <td colspan="8" align="center"><input type="submit" name="Update" value="UPDATE"></td>
                </tr>
            </table>
        </form>
    </body>
</html>

结果.php

<html>
    <body>

        <?php
        mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
        error_reporting(E_ALL);
        ini_set('display_errors',1);

        require_once('dbconnect.php');
        $query = "SELECT * FROM vw_events";
        $result = mysqli_query($conn, $query);
        $count = mysqli_num_rows($result);

        if ($_SERVER["REQUEST_METHOD"] == "POST")
        {
            $id = $_POST['event_id'];
            $title2 = $_POST['title'];
            $date2 = $_POST['date'];
            $speaker2 = $_POST['speaker'];
            $building2 = $_POST['building'];
            $room2 = $_POST['room'];

            for($i=0;$i<$count;$i++) {
                $sql="UPDATE events SET title='$title2[$i]', event_date='$date2[$i]', speaker='$speaker2[$i]', building='$building2[$i]', room='$room2[$i]' WHERE event_id='$id[$i]'";
                $result1=mysqli_query($conn, $sql);
            }
        }
        ?>
        <form name="form1" method="post" action="index.php">
            <table width="auto" border="1" cellspacing="1" cellpadding="5">
                <tr>
                    <td align="center"><strong>Event ID</strong></td>
                    <td align="center"><strong>Title</strong></td>
                    <td align="center"><strong>Topic</strong></td>
                    <td align="center"><strong>Description</strong></td>
                    <td align="center"><strong>Event Date</strong></td>
                    <td align="center"><strong>Speaker</strong></td>
                    <td align="center"><strong>Building</strong></td>
                    <td align="center"><strong>Room</strong></td>
                </tr>

                <?php                    
                    while($rows=mysqli_fetch_array($result)) {
                ?>

                <tr>
                    <td align="center">
                        <?php echo $rows['event_id'];?>
                    </td>
                    <td align="center">
                        <?php echo $rows['title']; ?>
                    </td>
                    <td align="center">
                        <?php echo $rows['topic_name']; ?>
                    </td>
                    <td align="center">
                        <?php echo $rows['topic_description']; ?>
                    </td>
                    <td align="center">
                        <?php echo $rows['event_date']; ?>
                    </td>
                    <td align="center">
                        <?php echo $rows['speaker_name']; ?>
                    </td>
                    <td align="center">
                        <?php echo $rows['building_name']; ?>
                    </td>
                    <td align="center">
                        <?php echo $rows['room_name']; ?>
                    </td>
                </tr>

                <?php
                    }
                ?>

                <tr>
                    <td colspan="8" align="center"><input type="submit" name="Return" value="Return"></td>
                </tr>
            </table>
        </form>
    </body>
</html>

另外如果有人可以给我一些关于如何在 results.php 中的数组上运行 htmlspecialchars 函数的指导,我会真的很感激。我已经尝试为每个数组创建一个 for 循环,但这不起作用。我尝试过使用 ->

<?php 
function htmlspecial_array(&$variable) {
    foreach ($variable as &$value) {
        if (!is_array($value)) { $value = htmlspecialchars($value);  }
        else { htmlspecial_array($value); }
    }
}

但这也不起作用,我尝试使用array_walk_recursive但无济于事。我想尝试做一些类似 W3Schools 的例子 W3Schools Form Validation页面底部显示使用 PHP 验证表单数据,然后给出了一个示例。

最佳答案

从 UPDATE 查询中获得的结果是数据库中受影响的行数。为了正确显示更新的数据,您需要在生成 HTML 之前从数据库中重新获取。您应该像这样重新排列 results.php 中的代码:

    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    error_reporting(E_ALL);
    ini_set('display_errors',1);
    require_once('dbconnect.php');

    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
        $id = $_POST['event_id'];
        $title2 = $_POST['title'];
        $date2 = $_POST['date'];
        $speaker2 = $_POST['speaker'];
        $building2 = $_POST['building'];
        $room2 = $_POST['room'];

        $query = "SELECT * FROM vw_events";
        $result = mysqli_query($conn, $query);
        $count = mysqli_num_rows($result);

        for($i=0;$i<$count;$i++) {
            $sql="UPDATE events SET title='$title2[$i]', event_date='$date2[$i]', speaker='$speaker2[$i]', building='$building2[$i]', room='$room2[$i]' WHERE event_id='$id[$i]'";
            $result1=mysqli_query($conn, $sql);
        }
    }

    $query = "SELECT * FROM vw_events";
    $result = mysqli_query($conn, $query);

旁注:如果您的数据敏感,您可能需要阅读 mysqli 准备好的语句,这样黑客就无法篡改您的查询。

关于 htmlspecialchars 的问题,请参阅 Stackoverflow“在多级数组上执行 htmlspecialchars”。

关于php - MySQL 表页未加载正确的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49801790/

相关文章:

html - HTML5 中的 & 和 & 有什么区别?

html - 使用 CSS 设置箭头的永久位置,而不影响表格单元格中的文本对齐

MySQL 如果差异小于 0,则选择 0,否则选择差异

php - 识别具有200个字段的行中的一个字段(mysql数据库)

php - 如何使用php获取同一页面中的输入数据

php - 如何在 WordPress 管理员的子菜单中创建多个子菜单?

php - 无法在 PHP 中设置默认时区

html - 为什么 `position: absolute` child 不尊重他们的亲戚 parent ?

mysqldump 通过 SSH 到本地计算机

mysql - 数据库架构