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 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用