doujiao1814 2015-01-26 23:22
浏览 41
已采纳

将文件夹中的文件名列表插入mysql

I retrieve a list of filenames from a folder and need to insert it into a mysql database using php.

I insert into the database and it starts building and won't stop. Its only about 20 items in the folder but it keeps looping into database:

<?php
$directory = "/xml/";
$results_array = array();

if(is_dir($directory)) {
    if($handle = opendir($directory)){
        while(($file = readdir($handle)) !== false) {
           $results_array[] = $file;
        }
        closedir($handle);
    }
}
foreach($results_array as $value) {
   $e_get = substr($value, 0, 16);
   $edate = substr($e_get, -6);
   $checkDB = $pdo->query("select `dateString` from `report`");
   $checkDB->fetchAll();
   foreach($checkDB as $checkItems){
       if($checkItems->dateString != $edate) {
          $pdo->query("insert into `report`(`dateString`) values
                       ('$edate');
       }
   }
}

Now I am trying a different way but I only get one item:

<?php
$directory = "/xml/";
$results_array = array();

if(is_dir($directory)) {
    if($handle = opendir($directory)){
        while(($file = readdir($handle)) !== false) {
           $results_array[] = $file;
        }
        closedir($handle);
    }
}
$edate = array();
foreach($results_array as $value) {
   $e_get = substr($value, 0, 16);
   $edate[] = substr($e_get, -6);
}
foreach($edate as $date) {
  $checkDB = $pdo->query("select `dateString` from `report`");
  foreach($checkDB as $checkItems) {
     if($checkItems->dateString != $date) {
          $pdo->query("insert into `report`(`dateString`) values
                       ('$edate');
     }
  }
}

Please help... Thanks!

  • 写回答

1条回答 默认 最新

  • dongtun2572 2015-01-27 19:33
    关注

    I'm assuming that the first part of the code is correctly populating $edate. After that you're reading the database unnecessarily, and you've used $edate in your second query, instead of $date.

    Because the database should be checking for uniqueness you should only need this:

    foreach($edate as $date) {
        $pdo->query("insert ignore into `report`(`dateString`) values ('$date')";
    }
    

    (INSERT IGNORE inserts a new entry, or ignores it if it detects an invalid request, such as a duplicate in a UNIQUE column.)

    However, this won't pick up any syntax or other errors that may occur, so you need to add some error checking. My suggestion:

    // Just after you open the PDO connection, set PDO to
    // throw an exception in the event of an error
    $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    
    // Then...
    try {
        foreach($edate as $date) {
            $pdo->query("insert ignore into `report`(`dateString`) values ('$date')";
        }
    } catch(PDOException $e) {
        echo 'Database error '.$e->getCode().': '.$e->getMessage();
        exit(1);
    }
    

    You could handle the exception differently, or not bother to catch it here and let the default exception handler catch it, if you have one.

    Note: you could do this in a single INSERT by imploding the $edate array.

    // No loop required
    $query = "insert ignore into `report`(`dateString`) values ('" . implode("'),('",$edate) . "')";
    $pdo->query($query);
    

    Security: I'm assuming that you have some control over the file names in the directory and that they'll be safe to use this way. If not, you have a susceptibility to SQL Injection in this code. You need either to use PDO::quote() on the values or better, switch to prepared statements.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件