我有文件名的數組INSERT到表基於陣列
$files = array();
$dirName = getcwd()."/../";
$dir = opendir('../'); // open the cwd..also do an err check.
while(false != ($file = readdir($dir))) {
if(($file != ".") and ($file != "..") and ($file != "index.php")and ($file != "img") and ($file != "__MACOSX")) {
$files[] = $file; // put in array.
}
}
我要插入一個新的記錄到項目表中的每個文件名如
foreach($files as $file) {
$filename = mysql_real_escape_string($file);
$query = "INSERT INTO `projects` (`id`, `name`) VALUES (NULL,$filename)";
$result = mysql_query($query, $connection);
echo $result;
}
但現在已經對數據庫產生影響。
'$ filename'是最有可能會是一個字符串;引用它。 –
你需要檢查'mysql_error($ connection)'來驗證這個動作的結果。正如@ Fred-ii-所說,字符串$ filename [將需要單引號](http://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-反推) –
另請參見[爲什麼不應該使用'mysql _ *()'函數](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。不用用過時的API編寫新代碼,這是[開始學習使用PDO](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers)的好時機,它支持準備語句(避免引用這樣的問題)並改進安全。 –