PHP导入数据完整性

时间:2015-12-30 18:28:03

标签: php mysql sql csv ms-access

我会尝试尽可能地解释情况:

  1. 我有脚本,将CSV文件数据导入MS Access数据库。
  2. 我有2个访问表:
  3. A)用户及其信息(ID,姓名,姓氏等)

    B)包含CSV文件数据的表

    问题是,从文件导入的数据(第2个表)包含用户名和姓氏。我想知道,如何在逐行读取csv文件的同时,检查名称行包含的内容,并从表1中分配userID而不是表2中的name和lastname。应该在导入时完成,因为,在每次导入时大约有3k行被导入。任何想法都赞赏。下面给出的图像。

    导入脚本:

    <?php
    function qualityfunction() {
    error_reporting(0); 
    require_once '/Classes/PHPExcel.php'; // (this should include the autoloader)
    require_once '/CLasses/PHPExcel/IOFactory.php';
    $excel_readers = array(
        'Excel5' , 
        'Excel2003XML' , 
        'Excel2007'
    );
    $files = glob('data files/quality/QA*.xls');
    $sheetname= 'AvgScoreAgentComments';
    if (count($files) >0 ) {
    foreach($files as $flnam) {
    $reader = PHPExcel_IOFactory::createReader('Excel5');
    $reader->setReadDataOnly(true);
    $reader->setLoadSheetsOnly($sheetname); 
    $path = $flnam;
    $excel = $reader->load($path);
    $writer = PHPExcel_IOFactory::createWriter($excel, 'CSV');
    $writer->save('data files/quality/temp.csv');
    
    /*
    $filename = basename($path);  
    if (strpos($filename,'tes') !== false) {
    echo 'true';
    }*/
                require "connection.php";
                $handle = fopen("data files/quality/temp.csv", "r");
                try {
          $import= $db->prepare("INSERT INTO quality(
                                  qayear,
                                  qamonth,
                                  lastname,
                                  firstname,
                                  score) VALUES(
                                  ?,?,?,?,?)");
        $i = 0;        
        while (($data = fgetcsv($handle, 1000, ",", "'")) !== FALSE) {
            if($i > 3) {
                $data = str_replace('",', '', $data); 
                $data = str_replace('"', '', $data); 
                $import->bindParam(1, $data[1], PDO::PARAM_STR);             
                $import->bindParam(2, $data[2], PDO::PARAM_STR);                
                $import->bindParam(3, $data[3], PDO::PARAM_STR);
                $import->bindParam(4, $data[4], PDO::PARAM_STR); 
                $import->bindParam(5, $data[7], PDO::PARAM_STR);            
                $import->execute();
            }
            $i++;
        }
    
        fclose($handle);
        $removal=$db->prepare("DELETE FROM quality WHERE score IS NULL;");
        $removal->execute();
                }
    catch(PDOException $e) {  
        echo $e->getMessage()."\n";
    
    }};
    

    数据表1(用户信息):

    enter image description here

    数据表2(导入CSV文件的数据)

    enter image description here

1 个答案:

答案 0 :(得分:0)

找到解决方案。谢谢你的帮助。

$lastname = "lastname";
$firstname = "firstname";
$showdata = $db->prepare("SELECT userID FROM users WHERE lastname= :lastname AND firstname= :firstname");
$showdata->bindParam(':lastname', $lastname);
$showdata->bindParam(':firstname', $firstname);
$showdata->execute();
$rowas= $showdata->fetch(PDO::FETCH_ASSOC);
echo $rowas['userID'];