If i uncomment out the print_r and copy and past it into workbench then run the query, it works fine but the code only inputs the last line of every table.
Code: Select all
$folderlocation = 'uploaded_data/';
$num = 0;
function prepare($v) {
$v = is_array($v) ? array_map("prepare", $v) : addslashes(trim($v));
return $v;
}
// Data
$items = array(
$items[0] = array(
'file' => 'ext001.csv',
'table' => "teams",
'fields' => "home_alley, alley_contact, team_name, contact_name, contact_number, playing_night, division",
),
$items[1] = array(
'file' => 'ext002.csv',
'table' => "fixtures",
'fields' => "fixture_id, played, division_num, home_alley, fixture_date, home_team_id, away_team_id",
),
$items[2] = array(
'file' => 'ext003.csv',
'table' => 'team_averages',
'fields' => 'ta_name, ta_games, ta_high, ta_low, ta_average, ta_rank',
),
$items[3] = array(
'file' => 'ext005.csv',
'table' => 'player_averages',
'fields' => 'player_name, player_age, player_team, player_sex, player_games, player_total, player_average, player_high, player_low, player_rank',
),
$items[4] = array(
'file' => 'ext008.csv',
'table' => "league_tables",
'fields' => "division, team_name, games, won, drawn, lost, points, rank",
),
$items[5] = array(
'file' => 'ext009.csv',
'table' => "results",
'fields' => "id, scored, division, alley, date, home_team, home_team_score, home_points, away_team, away_team_score, away_points",
),
$items[6] = array(
'file' => 'ext888.csv',
'table' => "highest_alley",
'fields' => "alley, date, name, score",
),
$items[7] = array(
'file' => 'ext999.csv',
'table' => "trophy_leaders",
'fields' => "trophy, date, name, score",
),
);
foreach ($items as $import) {
mysql_query("TRUNCATE TABLE ".$import['table']."");
$row = 1;
$handle = fopen($folderlocation.$import['file'], "r");
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
$data = prepare($data);
$num = count($data);
$row++;
//print_r ("INSERT INTO ".$import['table']." (".$import['fields'].") VALUES ('".implode("', '", $data)."'); ");
$query = "INSERT INTO ".$import['table']." (".$import['fields'].") VALUES ('".implode("', '", $data)."'); ";
}
fclose($handle);
if(mysql_query($query)) {
$tableName = ucwords (str_replace("_"," ", $import['table']));
echo "<span style=\"color: green;\">".$tableName." Updated</span>\r\n";
}
else {
echo "<span style=\"color: red;\">Failed!</span> Reason: ".mysql_error()."<br>\r\n";
}
$num++;
}
mysql_query("UPDATE lastupdate SET date = CURDATE()");
Kevin