我有一个以下查询,但不适用于特定情况。
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);
}
}
此代码扫描文件夹并将其插入数据库,但始终插入。如果文件夹不存在我想插入。请指教。
答案 0 :(得分:0)
为此,通过放置以下SQL来添加唯一索引。
ALTER TABLE
templates
ADD UNIQUE(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);
}
}