允许用户管理大量数据库行的最佳方式


Best way to allow users to manage large amount of DB rows

我有两个大表,如下:

第一个表:

  • tbl_properties
    • id
    • <
    • 地址/gh>城市
    • zip >时间戳

第二个表:

  • tbl_units
    • id
    • propertyID(将单元与属性关联)
    • 单位的名字
    • 时间戳

基本上,我试图发现最好的方法来管理这个(有时巨大的)属性和单位列表-在用户层面。这包括如下内容:

  1. 初始上传许多属性和单位(我想象用户可以从Excel中复制和粘贴,例如…但我不知道如何在后端正确解析信息)。
  2. 批量编辑属性/单元。
  3. 批量添加或删除某个属性的单元。

有什么想法吗?

对于delete,您可以通过使用"IN"关键字以这种方式进行批量删除,例如tbl_properties表:

DELETE FROM tbl_properties WHERE id IN (1,2,3...)

in关键字与下面的语句相同:

DELETE FROM tbl_properties WHERE id = 1 OR id = 2 OR id = 3

,你想要多少id的好处是,你会把delete语句删除所有的行,而不是只有一次循环,继续调用delete语句,将大量的工作过程为服务器,不幸的是,对于更新是不可能让你只能把每个语句的一个条件依然如果你要更新所有的列相同的信息为每一行。

解析你的excel有库可以帮助你管理它,并在解析过程中将每一列转换为数组,以便你轻松地管理它。

为了让用户一次上传多个属性,您可以使用csv文件。您必须为csv文件定义一个所有用户都必须遵循的结构。然后,您可以让用户通过file输入字段上传文件,然后使用fgetcsv()对其进行解析,并使用此信息更新数据库。一个小例子:

// handle_upload.php
if (isset($_FILES['propfile']) && $_FILES['propfile']['error'] == 0) {
   // try to open the csv file
   $fp = fopen($_FILES['propfile']['tmp_name'], "r");
   if ($fp) {
      // you must decide if you need the headers in the csv file
      // in this example there are no header so we just loop through
      // the data
      while ($record = fgetcsv($fp)) {
        // $record[0] to $record[5] should
        // contain the data you need to insert into your table
      }
      fclose($fp);
   }
}
// do something similar for the next file containing data
// for the tbl_units table