处理前检查条目

发布于 2024-11-09 04:58:18 字数 680 浏览 0 评论 0原文

嘿伙计们! 我有一个应该将图像添加到数据库中的脚本。它就是这样做的。唯一的问题是它不仅添加新图像,而且还添加现有图像。 我确信这对您来说是一项非常简单的任务,但对我来说却太多了!

因此,这段代码应该再次检查条目是否存在,如果不存在则添加图像。 谢谢 :)

    <?php

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);
}

$query = "INSERT INTO images (filename) VALUES ".implode(',', $images)." ";
if (!mysql_query($query)) {
    print mysql_error();
}
else {
    print "finished installing your images!";
}


?>

hey guys!
I have a script that should add images into the database. It does that. The only problem is that it does not only adds new images but also adds existing ones.
Im sure its a really simple task for you, for me its too much!

So again this code should check if an entry exists and then add the image if it does not exists.
Thank you :)

    <?php

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);
}

$query = "INSERT INTO images (filename) VALUES ".implode(',', $images)." ";
if (!mysql_query($query)) {
    print mysql_error();
}
else {
    print "finished installing your images!";
}


?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

巾帼英雄 2024-11-16 04:58:18

您可以使filename字段唯一(CREATE UNIQUE INDEX filename_idx ON images (filename)),然后只需将INSERT INTO images替换为INSERT忽略图像

作为额外的好处,您现在还可以为 filename 字段建立索引,这将加快 filenameSELECT 速度。

You can make the filename field unique (CREATE UNIQUE INDEX filename_idx ON images (filename)) then simply replace INSERT INTO images with INSERT IGNORE INTO images.

As an added bonus, you'll now also have the filename field indexed, which will speed up SELECTs by filename.

百善笑为先 2024-11-16 04:58:18

让我们来看看。要检查现有文件名是否已存储在数据库中,您必须执行以下操作:

  • 从数据库检索文件名
  • 检查当前文件是否在它们之间

PS:您应该需要您的 mysql.php 文件,没有它,你的脚本的其余部分将不起作用。

<?php
require_once('mysql.php');

$existing_files = array();
$qry = mysql_query("SELECT filename FROM images WHERE 1");
while($row = mysql_fetch_assoc($qry)) {
    array_push($existing_files, $row['filename']);
}


if ($handle = opendir('images')) {
    while (false !== ($file = readdir($handle))) {
        if($file!='.' && $file!='..' && !in_array($file, $existing_files)) {
            $images[] = "('".$file."')";
        }
    }

    closedir($handle);
}

$query = "INSERT INTO images (filename) VALUES ".implode(',', $images)." ";
if (!mysql_query($query)) {
    print mysql_error();
}
else {
    print "finished installing your images!";
}

Let's see. To check if exiting filenames are already stored in the database you must do the following:

  • retrieve filenames from the database
  • check if current file is between them

PS: You should require your mysql.php file, without it, the rest of your script won't work.

<?php
require_once('mysql.php');

$existing_files = array();
$qry = mysql_query("SELECT filename FROM images WHERE 1");
while($row = mysql_fetch_assoc($qry)) {
    array_push($existing_files, $row['filename']);
}


if ($handle = opendir('images')) {
    while (false !== ($file = readdir($handle))) {
        if($file!='.' && $file!='..' && !in_array($file, $existing_files)) {
            $images[] = "('".$file."')";
        }
    }

    closedir($handle);
}

$query = "INSERT INTO images (filename) VALUES ".implode(',', $images)." ";
if (!mysql_query($query)) {
    print mysql_error();
}
else {
    print "finished installing your images!";
}
靖瑶 2024-11-16 04:58:18

您应该将文件名添加为数据库中的唯一键。

或者,您可以使用这个 php 脚本:

$exists = mysql_query("SELECT * FROM images 
   WHERE filename LIKE '%".mysql_real_escape_string($images[0])."%'");
if(mysql_num_rows($exists == 0)){
  $query = "INSERT INTO images (filename) VALUES ".implode(',', $images)." ";
  if (!mysql_query($query)) {
    print mysql_error();
  }
  else {
    print "finished installing your images!";
  }
}

You should add filename as unique key in your database.

Or, you could use this php script:

$exists = mysql_query("SELECT * FROM images 
   WHERE filename LIKE '%".mysql_real_escape_string($images[0])."%'");
if(mysql_num_rows($exists == 0)){
  $query = "INSERT INTO images (filename) VALUES ".implode(',', $images)." ";
  if (!mysql_query($query)) {
    print mysql_error();
  }
  else {
    print "finished installing your images!";
  }
}
女中豪杰 2024-11-16 04:58:18

为什么不对文件执行 md5sum 并使用它来确保唯一性,然后执行选择以查看文件是否存在,如果不存在则插入并返回 id 作为批准状态,或者它是否已经存在现有记录的 ID。按文件名进行操作是可以的,除非几个人上传相同的文件但名称不同。这样做的缺点是,如果文件很大,那么需要花费一些资源来执行此操作。

Why not do an md5sum on the file, and use that for uniqueness, then do a select to see if the file is there, if it isn't insert if it it and return the id as the approve state, or if it already exists the id of the existing record. Doing it by filename is fine, unless, several people are uploading the same file but named differently. The downside to doing this is if the files are big, then it takes some of your resources to do this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文