※ 引述《flylinux (ㄚ琪)》之銘言:
: 有一點懂!
: $sql = "insert into table(";
: $astr = '';
: $bstr = '';
: foreach($_POST as $key => $value)
: {
: $astr .= $key.',';
: $bstr .= "'".$value."',";
: }
: $sql = $sql.rtrim($astr,",").") values (".rtrim($bstr,"',").")";
: 類似這樣吧!
這樣的話會把所有的$_POST都丟進query String裡
既然欄位是是資料表型式
那就這樣用
do{
$arr1[] = $row['name'];
$arr2[] = "'".$_POST[$row['name']]."'";
} while ($row = mysql_fetch_assoc($res))
$query1 = implode(",", $arr1);
$query2 = implode(",", $arr2);
$sql = INSERT INTO table ($query1) VALUES ($query2);
$res = mysql_query ($sql);
不過這樣有個問題是
欄位如果是可以新增刪除的話
代表table這個資料表的結構也是要隨之變動的
不太符合資料正規化...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.104.153.48