[英]Insert statement not working in scandir
I have a following query which is not working for a specific case. 我有以下查询不适用于特定情况。
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);
}
}
This code is scanning folders and inserting them to database but it is always inserting. 这段代码正在扫描文件夹并将其插入数据库,但始终会插入。 I want to insert if folder does not exist.
如果文件夹不存在,我想插入。 Please advise.
请指教。
For this purpose add a unique index by putting the following SQL. 为此,通过放置以下SQL添加唯一索引。
ALTER TABLE
templates
ADD UNIQUE (temp_loc
)ALTER TABLE
templates
添加唯一性(temp_loc
)
after that if your record exists then insertion will not be occur. 之后,如果您的记录存在,那么将不会插入。 now use your code
现在使用您的代码
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.