简体   繁体   English

删除所有除指定的内容

[英]Delete all except those specified

I have a program that sends an entry to my php script which is then added to the database and is updated instead if it already exists 我有一个程序可以将条目发送到我的php脚本,然后将其添加到数据库中,并且如果已经存在则进行更新

The program sends around 3000 entries and the php script saves the ids into a text file, after all the entries were sent the php script should delete all of the entries from the database except those with ids which are in the text file. 该程序发送了大约3000个条目,并且php脚本将id保存到文本文件中,在发送完所有条目之后,php脚本应该从数据库中删除所有条目,但那些id在文本文件中的条目除外。

Currently this is my code: 目前,这是我的代码:

if($finished == "yes")
{
  if(file_exists("completed.txt"))
  {
    $completed = file("completed.txt");
    unlink("completed.txt");
    $product = database::query("select * from " . DB_TABLE_PRODUCTS . ";");
    while($row = database::fetch($product))
    {
      if(!in_array($row["ewe"], $completed))
      {
        if($row["ewe"] != "" && $row["ewe"] != null)
        {
          database::query("delete from " . DB_TABLE_PRODUCTS . " where id = '" . $row["id"] . "';");
          database::query("delete from " . DB_TABLE_PRODUCTS_TO_CATEGORIES . " where product_id = '" . $row["id"] . "';");
          database::query("delete from " . DB_TABLE_PRODUCTS_INFO . " where product_id = '" . $row["id"] . "';");
          database::query("delete from " . DB_TABLE_PRODUCTS_PRICES . " where product_id = '" . $row["id"] . "';");
          $product_images = database::query("select * from " . DB_TABLE_PRODUCTS_IMAGES . " where product_id = '" . $row["id"] . "';");
          while($product_image = database::fetch($product_images))
          {
            if(is_file(FS_DIR_HTTP_ROOT . WS_DIR_IMAGES . $product_image['filename']))
            {
              unlink(FS_DIR_HTTP_ROOT . WS_DIR_IMAGES . $product_image['filename']);
            }
            functions::image_delete_cache(FS_DIR_HTTP_ROOT . WS_DIR_IMAGES . $product_image['filename']);
            database::query("delete from " . DB_TABLE_PRODUCTS_IMAGES . " where product_id = '" . $row["id"] . "' and id = '" . (int)$product_image['id'] . "' limit 1;");
          }
        }
      }
    }
  }
  exit();
}

But what happens with above is that all the records from the database get deleted and in my case, simply using DELETE FROM x WHERE x NOT IN is not good. 但是上面发生的事情是删除了数据库中的所有记录,在我的情况下,仅使用DELETE FROM x WHERE x NOT IN是不好的。

To sum it up i have a program that i run daily which sends about 3000 entries to my php script, which then adds/updates into the database and deleted those records from the database which are not present in the last batch of 3000 records. 综上所述,我有一个每天运行的程序,它将大约3000个条目发送到我的php脚本,然后将其添加/更新到数据库中并从数据库中删除那些在最后3000条记录中不存在的记录。

Anyone knows how can I do this? 有人知道我该怎么做吗?

Edit: 编辑:

I found the culprit, it seems like in_array($row["ewe"], $completed) isn't working corrently, it's returning false even tho it should return true. 我发现了罪魁祸首,似乎in_array($ row [“ ewe”],$ completed)不能正常工作,即使应该返回true,它也会返回false。

I manually checked $row["ewe"] and it was present in both the array and in the database, yet in_array returned false.. 我手动检查$ row [“ ewe”],它同时存在于数组和数据库中,但in_array返回false。

Edit 2: 编辑2:

Found the solution, seems like i had spaces/new lines in my array, i solved the problem by using: $completed = array_map("trim", file("completed.txt")); 找到了解决方案,似乎我的数组中有空格/换行符,我通过使用以下方法解决了该问题:$ completed = array_map(“ trim”,file(“ completed.txt”)); and now it's working like a charm :) 现在它像魅力一样工作了:)

Look into database relationships and constraints: https://dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html 查看数据库关系和约束: https : //dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html

In short, you can define that record A (eg OBJECT_DETAIL) cannot exist without the master record B (eg OBJECT). 简而言之,您可以定义没有主记录B(例如OBJECT)的记录A(例如OBJECT_DETAIL)就不存在。 Then deleting the master all details will also be removed. 然后删除母版的所有详细信息也将被删除。 This is how you ensure database integrity. 这是您确保数据库完整性的方式。

To intersect, union, or subtract records between batches, assign each batch a unique identifier. 要在批次之间相交,合并或减去记录,请为每个批次分配唯一的标识符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM