我已经构建了一个 PHP/MySQL CSV 上传器/更新器来更新高中橄榄球队的分数。
当我使用 Windows 7 在 XAMPP 上运行此脚本时,“加载数据输入文件”查询出现权限错误。我在 PHPMyAdmin 中检查了我的权限,并且用户拥有所有权限,所以不是这样。
我需要先解决这个问题。我最终通过直接在 PHPMyAdmin 上运行查询来上传所有内容。但是,当我尝试运行两个上传查询时,我的脚本超时。
我在这里要做的过程是上传文件并将记录导入新表(csv_data)。然后我想运行两个更新查询来用 csv_data 表中的新数据更新游戏表。
这两个查询的原因是,就家庭/访客而言,有时错误的团队位于错误的列中。我发现使用 CONCAT() 确实会减慢速度。
所以我需要先用“LOAD DATA INFILE”查询解决我的权限问题,然后想办法加快我的更新查询。有任何想法吗?
<?php
require_once('global.php');
if (array_key_exists('submit', $_POST)) {
if ($_FILES['csv']['error'] > 0){
echo "Error: " . $_FILES['csv']['error'] . "</br>" . "You have not selected a file or there is another error.";
} else {
$tmp = $_FILES['csv']['tmp_name'];
}
if (!$_FILES['csv']['type'] == 'text/csv'){
echo "Please select a CSV File";
} else {
$location = dirname(__FILE__) . '/csv/' . basename($_FILES['csv']['name']);
move_uploaded_file($tmp, $location);
}
$errors = array();
// Need to get this query to work properly regarding permissions
$query = $db->prepare("LOAD DATA INFILE ?
INTO TABLE csv_data
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(game_week, home_team, home_score, visitor_team, visitor_score)");
if ($query->execute(array($location))) {
try {
// Need to figure out how to use LIKE instead of = and do it quickly
$query_two = $db->prepare("UPDATE game g, csv_data c, team home_team, team visit_team
SET g.game_home_score = c.home_score,
g.game_visitor_score = c.visitor_score,
g.game_complete = 'Y'
WHERE g.game_week = c.game_week
AND home_team.team_name LIKE CONCAT('%', c.home_team, '%')
AND home_team.team_id LIKE CONCAT('%', g.game_home_team, '%')
AND visit_team.team_name LIKE CONCAT('%', c.visitor_team, '%')
AND visit_team.team_id LIKE CONCAT('%', g.game_visitor_team, '%')");
if (!$query_two->execute()) {
$errors[] = 'The first update query failed.';
}
$query_three = $db->prepare("UPDATE game g, csv_data c, team home_team, team visit_team
SET g.game_home_score = c.visitor_score,
g.game_visitor_score = c.home_score,
g.game_complete = 'Y'
WHERE g.game_week = c.game_week
AND home_team.team_name LIKE CONCAT('%', c.visitor_team, '%')
AND home_team.team_id LIKE CONCAT('%', g.game_home_team, '%')
AND visit_team.team_name LIKE CONCAT('%', c.home_team, '%')
AND visit_team.team_id LIKE CONCAT('%', g.game_visitor_team, '%')");
if (!$query_three->execute()) {
$errors[] = 'The second update query failed.';
}
} catch(PDOException $e) {
echo $e->getMessage();
}
} else {
$errors[] = 'CSV Upload Query Failed.';
}
}
?>