douxie9347 2016-05-12 22:09
浏览 30
已采纳

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

I am tired of this code. Really. I have been working yesterday to get this fixed and it's just not working. I would appriciate if anyone would give them their input.

I found an old CMS with some great features. However, that CMS is from 2008 and is still running on MySQL and is not being updated anymore. So I took a few scripts out of it. One is being able to adjust multiple database rows on and off with just 1 click on the submit button.

The database looks like this.

The value means on or off. In dutch it is aan and uit. I'm making a Dutch CMS so I'm creating it in Dutch language for the most part so my customer can understand it too.

The code from the 2008 CMS looks like this: http://pastebin.com/EkmB8rFr (I'm pasting it in Pastebin because it's messy code and SO is too small for this).

So my job is to convert this 2008 code into 2016 code with all the SQL injections fixed and all. Here's my result:

    <?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
}

This code is just not working like the 2008 code and I just don't know what is going on. The error is that it puts the ID in the database and not the value of the <select>.


The output should be like this:

  • 写回答

1条回答 默认 最新

  • duanluan3651 2016-05-12 22:18
    关注

    It looks like some things are mixed up in your update. I think this should work better:

    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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?