Aram Mkrtchyan
Aram Mkrtchyan

Reputation: 2700

php update radio button state in db

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

enter image description here

Upvotes: 0

Views: 3585

Answers (1)

Barmar
Barmar

Reputation: 780655

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.

Upvotes: 1

Related Questions