php更新单选按钮状态在db [英] php update radio button state in db

查看:148
本文介绍了php更新单选按钮状态在db的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的php,我刚开始学习php和有一些问题。
在管理面板中我有一些添加信息的形式,之后在输出我有单选按钮,用于选择哪一行显示到网站。问题是在更新功能查询是确定,但它不工作,我需要你帮助。

I am new in php, I just starting learn php and have some problems. In admin panel I have some form for adding info, after that in output I have radio buttons for selecting which row show into website. The problem is in update function the query is ok but it doesn't work, and I need you help.

谢谢。

提交if。

 if(isset($_POST['update_info'])){
        $selected_info=$_POST['selected'];
        $selected_id = $_POST['id'];
        updateHomeInfo($selected_info, $selected_id);
    }

输出添加的行。

<div class="home-output">
        <form action="/admin/" method="post">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <td>#</td>
                        <td>slected</td>
                        <td>title</td>
                        <td>descriptiom</td>
                        <td></td>
                        <td></td>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($homeInfo as $row) { ?>
                    <tr>
                        <td><?php echo $row['id']; ?><input type="hidden" name="id" value="<?php echo $row['id']; ?>"></td>
                        <td><input type="radio" name="selected" value="home-info" <?php if($row['selected'] == 1) echo "checked"; ?> /></td>
                        <td><?php echo $row['title']; ?></td>
                        <td><?php echo $row['description']; ?></td>
                        <td></td>
                        <td></td>
                    </tr>
                    <?php } ?>
                    <tr>
                        <td>
                            <input type="submit" class="btn btn-default" name="clear_info" value="delete all">
                        </td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td>
                            <input type="submit" class="btn btn-default" name="update_info" value="update">
                        </td>
                    </tr>
                </tbody>
            </table>
        </form>
    </div>

并更新函数。

function updateHomeInfo($selected_info, $selected_id) {
    global $db;
    $db->exec("UPDATE home SET selected = '$selected_info' where id = '$selected_id'");
}

推荐答案

所有隐藏的输入都具有相同的名称;它们都将被提交,并且 $ _ POST ['id'] 的值将只是最后一个,而不是所选单选按钮旁边的值。所有单选按钮的值也相同。

All your hidden inputs have the same name; they'll all be submitted, and the value of $_POST['id'] will just be the last one, not the one next to the selected radio button. And all the radio buttons have the same value as well.

您应该将ID放入单选按钮的值。您只需要一个隐藏输入的副本,它可以包含所选信息。

You should put the ID into the value of the radio button. You only need one copy of the hidden input, and it can contain the selected info.

<form action="/admin/" method="post">
    <input type="hidden" name="selected" value="home-info">
    <table class="table table-hover">
        ...
        <td><?php echo $row['id']; ?>
        <td><input type="radio" name="id" value="<?php echo $row['id']; ?>" <?php if($row['selected'] == 1) echo "checked"; ?> /></td>
        ...

其实,似乎你不需要 selected 输入,如果选择是一个布尔值。它应该是:

Actually, it seems like you don't need the selected input at all, if selected is a boolean. It should just be:

function updateHomeInfo($selected_id) {
    $db->exec("UPDATE home SET selected = (id = '$selected_id')");
}

选择 到所选其他ID的 1 和所有其他ID的 0

This will set selected to 1 for the selected ID, and 0 for all the other IDs.

这篇关于php更新单选按钮状态在db的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆