我还在学习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>
但是当我点击按钮时唯一更新的是最后一行。
有人可以指导我吗?
答案 0 :(得分:1)
这是您的代码在客户端
中的样子 <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*/
}
}