我已經搜索了一個針對我的問題的simliar解決方案,但是我正在努力尋找它,所以請原諒,如果這已被問到。我有我的PHP代碼,這樣根據鍵值將數組插入到mysql中
foreach($data['cells'] as $row) {
print_r($row);
}
產生
Array
(
[1] => ASSET ID
[2] => SERIAL IN CAPS
[3] => COMPANYASSETTAG
[4] => DATE RCVD
[5] => MFR
[6] => Type
[7] => MODEL
[8] => PRINTER MODEL COMMENTS
[9] => PART NUMBER
[10] => R ID
[11] => GRADE
[12] => PRICE
[13] => COLOR CAPABLE (Color or Monochrome)
[14] => COSMETICALLY ACCEPTABLE (Yes or No)
[15] => PRINTERCABLEINCLUDED (Yes or No)
[16] => PRINTER TECHNOLOGY (Laser, Inkjet, 4-1 Laser, 3-1 Laser, 4-1 Ink Jet, 3-1 Ink Jet, Dot Matrix, Plotter, Solid Ink, Thermal)
[17] => DUPLEX (Yes or No)
[18] => MULTIFUNCTION (Yes or No)
[19] => COMMENT (reason)
[20] => COSMETICS COMMENT
[21] => PURCHASE ORDER # (Trailer #)
[22] => WAYBILL#
)
Array
(
[1] => CNGYF04230
[2] => CNGYF04230
[3] => MISSING
[4] => 28/12/2012
[5] => Hewlett Packard
[6] => Multi-Function Printers
[8] => 4345X
[9] => Q3943A
[11] => G
[13] => Monochrome
[14] => Yes
[15] => No
[16] => Laser
[17] => Yes
[18] => Yes
[21] => TRDS293
[22] => HM693800
)
正如你可以看到關鍵的第二陣列中失蹤的人相比,上述陣列。這些對應於列。第二部分缺失的原因是因爲這些字段不包含任何數據。我希望能夠根據鍵將這些插入到mysql表中相應的字段中。
例如
foreach($data['cells'] as $row) {
//print_r($row);
$sql = "INSERT INTO table (asset_id,serial_in-caps...) VALUES ('$row[0]','$row[1]'...)";
}
你應該*永遠不會*以這種方式生成你的查詢字符串。 http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-php –