我正在向查詢傳遞一個數組。但是,我似乎無法添加每個值`。使用PHP將數據插入到MySQL數據庫
陣列林的傳球是:
Array (
[name] => Name
[address_1] => Address 1
[address_2] => Address 2
[address_3] => Address 3
[address_4] => Address 4
[post_code] => Post COde
[proptype] => rent
[style] => house
[beds] => 1
[bathrooms] => 1
[garden] => 1
[furnished] => yes
[deposit] => Deposit
[available] => 10/18/2013
[description] => Description
)
下面是我的代碼:
foreach ($data as $column => $value) {
$columns .= ($columns == "") ? "" : ", ";
$columns .= $column;
$values .= ($values == "") ? "" : ", ";
$values .= $value;
}
$sql = "INSERT INTO $table ($columns) VALUES ($values)";
echo "--->" . $sql;
exit;
我知道我需要附上各自的價值,但我看不到,我需要這樣做。
謝謝你的時間。
除非你的值都是數值,你必須用單引號括起來,這對數值也是安全的。當插入數字字段時,'1'將被轉換爲1 – Loopo
!請! !使用! !PDO! http://php.net/manual/en/book.pdo.php不要問爲什麼,只是做。你可以稍後閱讀。 – nietonfir
同意@nietonfir :-) – dimi