[英]Insert statement not working in scandir
我有以下查詢不適用於特定情況。
function scantemp(){
if ($dir = opendir('../themes/')) {
$onlyfolder = array('.', '..');
while (false !== ($files = readdir($dir))) {
if (!in_array($files, $onlyfolder)) {
$insert = mysql_query("INSERT INTO `templates` (temp_loc) VALUES ('$files')");
}
}
closedir($dir);
}
}
這段代碼正在掃描文件夾並將其插入數據庫,但始終會插入。 如果文件夾不存在,我想插入。 請指教。
為此,通過放置以下SQL添加唯一索引。
ALTER TABLE
templates
添加唯一性(temp_loc
)
之后,如果您的記錄存在,那么將不會插入。 現在使用您的代碼
function scantemp(){
if ($dir = opendir('../themes/')) {
$onlyfolder = array('.', '..');
while (false !== ($files = readdir($dir))) {
if (!in_array($files, $onlyfolder)) {
$insert = mysql_query("INSERT INTO `templates` (temp_loc) VALUES ('$files')");
}
}
closedir($dir);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.