单击一次提交更改数据库值 PHP

Change Database Values with one submit click PHP

我厌倦了这段代码。真的。我昨天一直在努力解决这个问题,但就是没用。如果有人愿意给他们意见,我会很感激。

我找到了一个具有一些很棒功能的旧 CMS。但是,该 CMS 是 2008 年的,在 MySQL 上仍然是 运行,并且不再更新。所以我从中提取了一些脚本。一个是能够调整多个数据库行 onoff,只需单击提交按钮 1 次。

数据库看起来像这样。

该值表示打开或关闭。在荷兰语中是 aanuit。我正在制作荷兰语 CMS,所以我大部分时间都是用荷兰语创建的,这样我的客户也能理解。

2008 CMS 中的代码如下所示:http://pastebin.com/EkmB8rFr(我将其粘贴到 Pastebin 中,因为它是混乱的代码,而且太小了)。

所以我的工作是将这个 2008 年的代码转换为 2016 年的代码,并修复所有 SQL 注入。这是我的结果:

    <?php
    if(isset($_POST['opslaan'])) {
        $stmt = $dbConnection->prepare('SELECT * FROM settings');
        $stmt->execute();
        $result = $stmt->get_result();
        while ($row = $result->fetch_assoc()) {
            $id = $_POST[$row["id"]];
            $row = $row["id"];
            $stmt1 = $dbConnection->prepare('UPDATE settings SET value = ? WHERE id = ?');
            $stmt1->bind_param('ss', $id, $id);
            $stmt1->execute();
        }

        //header('Location: index.php?page=instellingen');
    } else {
        $stmt = $dbConnection->prepare('SELECT * FROM settings ORDER BY id ASC');
        $stmt->execute();

        $result = $stmt->get_result();
    ?>
    <form method="POST" action="">
        <table>
        <tr>
            <th>Naam</th>
            <th>Laatst gewijzigd</th>
            <th>Waarde</th>
        </tr>
        <?php
    while ($row = $result->fetch_assoc()) {
    ?>
        <tr>
            <td><?php echo $row["code"]; ?></td>
            <td><?php echo $row["updated"]; ?></td>
            <td>
                <select name="<?php echo $row["id"]; ?>">
                    <option value="aan"<?php if($row["value"] == "aan") {echo ' selected';} ?>>Aan</option>
                    <option value="uit"<?php if($row["value"] == "uit") {echo ' selected';} ?>>Uit</option>
                </select>
            </td>
        </tr>
    <?php } ?>
        </table>
    <input type="submit" value="Opslaan" name="opslaan">
    </form>
    <?php
}

这段代码不像 2008 年的代码那样工作,我只是不知道发生了什么。 错误是它将 ID 放入数据库而不是 <select> 的值。


输出应该是这样的:

您的更新中似乎混淆了一些内容。我认为这应该更好用:

while ($row = $result->fetch_assoc()) {

    $id = $row["id"];                       // get the id from the row
    $value = $_POST[$id];                   // find the corresponding POST value

    $stmt1 = $dbConnection->prepare('UPDATE settings SET value = ? WHERE id = ?');

    $stmt1->bind_param('ss', $value, $id);  // Bind the correct variables here
    $stmt1->execute();
}