1

这可能听起来有点令人困惑,也许我正在做的事情根本不对,但这里是。

我们的用户可以上传包含他们学校数据的 .csv 文件,这些文件是从名为 SIMS 的外部管理系统导出的。一旦他们上传了那个文件,我想运行一个 .sql 脚本,用他们在这个文件中提供的信息更新我们的数据库。我所做的是,在上传文件页面上(就像一个魅力一样),创建一个包含上传文件的文件路径和名称的会话变量,如下所示(文件路径被空白):

$target_path = "xxx"; 

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
   {
     echo "<p>The file ".  basename( $_FILES['uploadedfile']['name']). " has been uploaded.</p>" ;
     echo "<p><a href='sqltest.php'>Click here</a> to upload your files into the database.</p>" ;

     $_SESSION['file'] = $target_path ;
   }

下面是我在 PHP 中操作的 .sql 文件的一部分,其中包含会话变量。因为这个填充脚本中的大部分都使用相同类型的命令,所以向您展示所有 973 行代码可能没有意义(当然,页面顶部有一个 session_start() 处于活动状态)。

$filename = $_SESSION['file'] ;

mysqli_query($dbc, 'SET foreign_key_checks = 0') ;

$populate =

"CREATE TEMPORARY TABLE IF NOT EXISTS `mldb`.`TempSchool`
    (
        `CentreNo` INT UNSIGNED NOT NULL,
        `School` VARCHAR(255) NULL,
        `Street` VARCHAR(255) NULL,
        `Town` VARCHAR(255) NULL,
        `County` VARCHAR(255) NULL,
        `Postcode` VARCHAR(10) NULL,
        `Tel` VARCHAR(45) NULL,
        `URL` VARCHAR(512) NULL,
        `Email` VARCHAR(255) NULL,
        `Headteacher` VARCHAR(255) NULL,
        `LEA` VARCHAR(45) NULL,
        `LEANo` INT UNSIGNED NULL,
        `EstablishmentNo` INT UNSIGNED NULL,
        `URN` INT UNSIGNED NULL,
        `Governance` VARCHAR(45) NULL,
        `Phase` VARCHAR(45) NULL,
        PRIMARY KEY (`CentreNo`)
    )
    ENGINE = InnoDB ;

LOAD DATA INFILE '$filename'        
IGNORE INTO TABLE `TempSchool`                  
FIELDS TERMINATED BY ' , ' 
OPTIONALLY ENCLOSED BY ' \" '
LINES TERMINATED BY ' \r\n '                                
IGNORE 1 LINES  (etc...)                                            

我在代码 (LOAD DATA INFILE '$filename') 的第 25 行遇到错误,显示以下内容:

Invalid query:You have an error in your SQL syntax; check the manual that corresponds to your MySQL   
server version for the right syntax to use near 'LOAD DATA INFILE   
'C:/Users/Public/Dropbox/mlwebfiles/Trial/uploads/MarksLive Set' at line 25

据推测,它与以下事实有关,即使它识别存储在 $_SESSION['file'] 变量中的文件路径和文件名,但它实际上并没有对它做任何事情。有没有办法通过 PHP 为文件名设置变量来操作这个 .sql 文件?非常感谢!

4

1 回答 1

0

经过漫长而艰苦的搜索,我们找到了一个(我们认为是唯一的)解决方案:创建一个函数来分别循环遍历这些“迷你查询”中的每一个,这似乎更快乐现在!我们实际上之前遇到过这个建议,但我们最初拒绝了它,因为它会(并且确实)需要很多时间来分离 973 行代码来拆分所有单独的数据库添加......

$populate = 
"
    CREATE TEMPORARY TABLE IF NOT EXISTS `mldb`.`TempSchool`
    (
        `CentreNo` INT UNSIGNED NOT NULL,
        `School` VARCHAR(255) NULL,
        `Street` VARCHAR(255) NULL,
        `Town` VARCHAR(255) NULL,
        `County` VARCHAR(255) NULL,
        `Postcode` VARCHAR(10) NULL,
        `Tel` VARCHAR(45) NULL,
        `URL` VARCHAR(512) NULL,
        `Email` VARCHAR(255) NULL,
        `Headteacher` VARCHAR(255) NULL,
        `LEA` VARCHAR(45) NULL,
        `LEANo` INT UNSIGNED NULL,
        `EstablishmentNo` INT UNSIGNED NULL,
        `URN` INT UNSIGNED NULL,
        `Governance` VARCHAR(45) NULL,
        `Phase` VARCHAR(45) NULL,
        PRIMARY KEY (`CentreNo`)
    )
    ENGINE = InnoDB ;

" ;
populate ($dbc, $populate);


$populate = 
"   
    LOAD DATA INFILE '$path'
    IGNORE INTO TABLE `mldb`.`TempSchool`
    FIELDS TERMINATED BY ','
    OPTIONALLY ENCLOSED BY '\"'
    LINES TERMINATED BY '\\r\\n'
    IGNORE 1 LINES
        (@AdNo, @UPN, @ULN, @UCI, @CandidateNo, @LegalSurname, @LegalForename, 
        @PreferredSurname, @PreferredForename, @Gender, @DOB, @Email,
        @InCare, @EverInCare, @FSM, @FSMEver6, @EAL, @SENStatus, @AMA,
        @Attendance, @RegGroup, @YearGroup, @EnteredYearDate, 
        @Class, @Subject, @Staff, @Initials, 
        CentreNo, School, Street, Town, County, Postcode, Tel, URL,
        Email, Headteacher, LEA, LEANo, EstablishmentNo, Governance, Phase)
" ;
populate ($dbc, $populate);
于 2013-11-06T10:22:21.133 回答