我從數據庫中獲取一些值。並希望更新數據庫表尊重他們的ID。如何插入多個複選框值尊重他們的ID
這裏是我的代碼:
<?php
$sql = "SELECT * FROM tbl_ads ORDER BY ads_id DESC LIMIT $START, $LIMIT";
$result = mysql_query($sql);
$num = mysql_num_rows($result);
if($num > 0){
while($row = mysql_fetch_array($result)){ ?>
<tr bgcolor="#E1E1E1">
<td align="left" valign="top" bgcolor="#F1F1F1" class="BlackText2">
<?php echo $row['title'];?>
</td>
<td align="left" valign="top" bgcolor="#F1F1F1" class="BlackText2">
<?php echo substr($row['description'], 0, 40);?>
</td>
<td align="left" valign="top" bgcolor="#F1F1F1">
<?php echo $row['mywebsite'];?>
</td>
<td align="center" valign="top" bgcolor="#F1F1F1">
<input type="hidden" name="ads_id[]" value="<?php echo $row['ads_id'];?>">
<input type="checkbox" name="feature[]" value="feature">
</td>
<?php
if(isset($_REQUEST['submit'])){
$feature = $_POST['feature'];
$ads_id = $_REQUEST['ads_id'];
if(empty($feature)){
echo("You didn't select any buildings.");
}else{
$N = count($feature);
$M = count($ads_id);
echo("You selected $N door(s): ");
for($i=0 ; $i < $N ; $i++){
for($j=0 ; $j < $M ; $j++){
echo ($feature[$i] . " ");
echo $update_feature = "UPDATE tbl_ads SET `featureads` = '".$feature[$i]."' WHERE ads_id ='".$ads_id[$j]."'";
$result_feaure = mysql_query($update_feature);
}
}
}
}
?>
但我的問題是,當我更新featureads列尊重ads_id列,那麼所有列都被選中。
如何解決這個問題?
不要使用'bgcolor'或'align'屬性,而使用CSS風格你的輸出。我建議不要使用'mysql_'函數,最好使用[PDO](http://php.net/manual/en/book.pdo.php)或者至少[MySQLi](http://php.net) /manual/en/book.mysqli.php)。另外,$ START和$ END變量來自哪裏?您的代碼很少有其他問題... – Cobby 2011-04-16 09:49:33
@Cobby感謝您的建議,$ START和$ END變量來自其他sql。其實我的問題是,當我檢查1或2複選框和「$ update_feature」sql運行所有功能組件行更新,而不是那2個選定的字段 – Moumita 2011-04-16 10:03:35