doumei4964 2015-12-30 18:28
浏览 25
已采纳

PHP导入数据完整性

I will try to explain situation as well as possible:

  1. I have script, that imports CSV file data to MS Access database.
  2. I have 2 access Tables:

A) Users and their information(ID, name, last name etc.)

B) Table which contains data from CSV file

Problem is, data imported from file, (2nd table) contains Users name and lastname. I want to get idea, how to, while reading csv file line by line, check what name line contains, and assign userID from table 1 instead of name and lastname on table 2. It should be done while importing, because, on each import there are roughly 3k lines being imported. Any ideas appreciated. Images given bellow.

Import script:

<?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()."
";

}};

Data table 1 (Users info):

enter image description here

Data table 2 (In which data from CSV file is imported)

enter image description here

  • 写回答

1条回答 默认 最新

  • dongteng2534 2016-01-02 21:00
    关注

    Found a solution. Thanks for help.

    $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'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 基于52单片机的酒精浓度检测系统加继电器和sim800
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答