gpt4 book ai didi

php - 如何检查文件的数据是否已经在数据库中

转载 作者:太空宇宙 更新时间:2023-11-03 10:58:13 26 4
gpt4 key购买 nike

我目前遇到一个问题,如果我多次运行此 php,那么一旦我当前在数据库中的图像数据将重复。例如,我将 5 张图片上传到我的上传文件,然后单击脚本,一切正常。当我添加另一张图片并再次单击脚本时,我的数据库中有 6 张图片变成了 11 张。我想知道如何修改我的代码才能解决这个问题。

include('mysql.php');

if ($handle = opendir('images')) {

/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
if($file!='.' && $file!='..') {
$images[] = "('".$file."')";
}
}

closedir($handle);
}

/* insert new record into the table images with the filename */
$query = "INSERT INTO images (filename) VALUES ".implode(',', $images)." ";
if (!mysql_query($query)) {
print mysql_error();
}
else {
print "finished installing your images!";
}

最佳答案

您可以将“文件名”列设为唯一键并使用INSERT IGNORE

有关插入忽略的更多信息:http://dev.mysql.com/doc/refman/5.5/en/insert.html

关于php - 如何检查文件的数据是否已经在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18393957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com