2011年1月8日 星期六

【PHP】foreach(){}迴圈與form表單

在php中善用foreach(){}迴圈與form表單可以輕鬆寫出多筆欄位存入資料庫的問題。

表單中有許多欄位需要填寫,
可是要一個一個寫入資料庫就太費時了,
也會使得語法落落長一大串,拖慢速度。

以下以排序作為範例。

ex.

<form action="" id="form1" method="post">
<?php do{
<input name=\"$id\" type=\"text\" value=\"$order\" />
}while($row=mysql_query($query_str))
?>
<input id="button" name="button" type="submit" value="排序" />
</form>

<?php
if(count($_POST)>0){
foreach($_POST as $id=>$order){
$str="UPDATE `sifc` SET `order` = '$order' WHERE `sifc`.`id` ='$id' LIMIT 1";
mysql_query($str);}
}
?>
祝大家成功囉!!下面圖片是類似範例所做出的圖檔。
來去睏啊~



沒有留言:

張貼留言