为所有值创建一个更新按钮


create 1 update button for all value

我还在学习php,我有这个脚本,我需要修改。在这个脚本中有多个更新按钮,我想创建另一个按钮来更新所有的值。

这是我的脚本

<div class='tableContainer'>
                    <table>
                        <thead>
                            <th width='100px'>Pasaran</th>                          
                            <th width='150px'>Status</th>
                        </thead>
                        <tbody>
                            <?php foreach($pasaran as $b){ ?>
                            <tr>
                                <td width='101px'><?php echo $b['keterangan']; ?></td>
                                <td width='151px'>
                                    <form action='<?php echo base_url('home/update_pasaran'); ?>' method='POST' class='uk-form'>
                                        <input type='hidden' name='id_pasaran' value='<?php echo $b['id_pasaran']; ?>'/>
                                        <select name='status_pasaran' class='uk-form-small'>
                                            <option <?php if($b['status_pasaran']=='Offline') echo 'selected'; ?> >Offline</option>
                                            <option <?php if($b['status_pasaran']=='Online') echo 'selected'; ?> >Online</option>
                                        </select>
                                        <button class='uk-button uk-button-primary uk-button-small'>Update</button>
                                    </form>
                                </td>
                            </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>
我试着添加这个脚本
<form action='<?php echo base_url('home/update_pasaran'); ?>' method='POST' class='uk-form'>
                                        <input type='hidden' name='id_pasaran' value='<?php echo $b['id_pasaran']; ?>'/>
                                        <select name='status_pasaran' class='uk-form-small'>
                                            <option <?php if($b['status_pasaran']=='Offline') echo 'selected'; ?> >Offline</option>
                                            <option <?php if($b['status_pasaran']=='Online') echo 'selected'; ?> >Online</option>
                                        </select>
                                        <button class='uk-button uk-button-primary uk-button-small'>Update</button>
                                    </form>

但是当我点击按钮时唯一更新的是最后一行

有人能给我指路吗?

您的代码在客户端应该是这样的

            <div class='tableContainer'>
                <form action='post.php' method='POST' class='uk-form'>
                    <table>
                        <thead>
                            <th width='100px'>Pasaran</th>                          
                            <th width='150px'>Status</th>
                        </thead>
                        <tbody>
                            <?php foreach($pasaran as $b){ ?>
                            <tr>
                                <td width='101px'><?php echo $b['keterangan']; ?></td>
                                <td width='151px'>
                                    <?php if($b['status_pasaran']=='Offline'){?>
                                    <td><input type="checkbox" name="Online[]" value="DB_ID"></td>
                                    <?php } else{ ?>
                                    <td><input type="checkbox" name="Offline[]" value="DB_ID"></td>
                                    <?php }?>
                                </td>
                            </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                    <input type="submit" value="APPLY CHANGES">
                </form>
            </div>

post.php文件应该像这样:

$online_array = $_POST["Online"];
$offline_array = $_POST["Offline"];
if(isset($online_array)&&!empty($online_array)){
    foreach($online_array as $object_to_update){
        /*Your query for the data base goes here*/
    }
}
if(isset($offline_array)&&!empty($offline_array)){
    foreach($offline_array as $object_to_update){
        /*Your query for the data base goes here*/
    }

}