php update radio button state in db

无人久伴 提交于 2019-12-12 05:24:50

问题


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.

thanks.

submit if.

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

output added rows.

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

and update function.

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


回答1:


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.

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>
        ...

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')");
}

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



来源:https://stackoverflow.com/questions/27615121/php-update-radio-button-state-in-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!