简体   繁体   English

PHP导入的数据完整性

[英]PHP imported data integrity

I will try to explain situation as well as possible: 我将尽力解释情况:

  1. I have script, that imports CSV file data to MS Access database. 我有脚本,可以将CSV文件数据导入MS Access数据库。
  2. I have 2 access Tables: 我有2个访问表:

A) Users and their information(ID, name, last name etc.) A)用户及其信息(ID,名称,姓氏等)

B) Table which contains data from CSV file B)包含来自CSV文件的数据的表

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. 我想知道如何操作,一边逐行读取csv文件,检查包含的名称行,然后从表1中分配用户ID,而不是表2中的名称和姓氏。这应该在导入时完成,因为在每次导入时大约有3k条线被导入。 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()."\n";

}};

Data table 1 (Users info): 数据表1(用户信息):

在此处输入图片说明

Data table 2 (In which data from CSV file is imported) 数据表2(从CSV文件导入数据)

在此处输入图片说明

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'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM