dongzhang7157 2016-05-12 17:08
浏览 30
已采纳

PHP选择提交不起作用

I have rows in my database and it has a column called value. I'm making a page to update the value to aan (English: on) or uit (English: off).

However, when submitted, there's a 1 in the value column instead of aan or uit.

The ID of the row is 1, so that's why there's a 1 but I don't know what is going wrong and why it's not submitting the aan or uit.

What's going wrong?

    <?php
    $stmt = $dbConnection->prepare('SELECT * FROM settings ORDER BY id ASC');
    $stmt->execute();

    $result = $stmt->get_result();
    if(mysqli_num_rows($result) > 0) {
        ?>
    <form method="POST" action="">
        <table>
        <tr>
            <th>Naam</th>
            <th>Laatst gewijzigd</th>
            <th>Waarde</th>
        </tr>
        <?php
    while ($row = $result->fetch_assoc()) {
        if(isset($_POST["opslaan"])) {
            $id = $row["id"];
            $stmt = $dbConnection->prepare('UPDATE settings SET value = ? WHERE id = ?');
            $stmt->bind_param('ss', $id, $id);
            $stmt->execute();
        }
    ?>
        <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
    } else {
        echo "<p>Er zijn nog geen instellingen.</p>";
    }
}
    ?>
  • 写回答

1条回答 默认 最新

  • drqwbh2150 2016-05-12 17:14
    关注

    Why don't you change the name of the Select instead like so:

        <td>
            <select name="aan_uit">  <!-- <== GIVE THE SELECT THE NAME aan_uit -->
                <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>       
    

    PHP

    <?php
    
    
        if(isset($_POST["opslaan"])) {
            $id      = $row["id"];
            $aanUit  = htmlspecialchars(trim($_POST["aan_uit"])); //<== EXTRACT "aan_uit" FROM THE POST VARIABLE.
            $stmt    = $dbConnection->prepare('UPDATE settings SET value = ? WHERE id = ?');
            $stmt->bind_param('ss', $aanUit, $id);  // <== SET TO $aanUit, NOT $id
            $stmt->execute();
        }
    

    So now the entire Form-Block should look like so:

        <form method="POST" action="">
            <table>
                <tr>
                    <th>Naam</th>
                    <th>Laatst gewijzigd</th>
                    <th>Waarde</th>
                </tr>
                <?php
                    while ($row = $result->fetch_assoc()) {
                        if(isset($_POST["opslaan"])) {
                            $id     = $row["id"];
                            $aanUit = $row["aan_uit"];
                            $stmt   = $dbConnection->prepare('UPDATE settings SET value = ? WHERE id = ?');
                            $stmt->bind_param('ss', $aanUit, $id);
                            $stmt->execute();
                        }
                        ?>
                        <tr>
                            <td><?php echo $row["code"]; ?></td>
                            <td><?php echo $row["updated"]; ?></td>
                            <td>
                                <select name="aan_uit">
                                    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效