-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertPlayers.php
44 lines (40 loc) · 2.12 KB
/
insertPlayers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
include './includes/dbconfig.php';
$upload_path = './uploads/';
$newname = rand(0000, 9999) . basename($_FILES["importInputFile"]["name"]);
$target_file = $upload_path . $newname;
$filename = $newname;
$filenameArray = explode(".", $filename);
$extension = end($filenameArray);
if ($extension == 'csv') {
if (move_uploaded_file($_FILES["importInputFile"]["tmp_name"], $target_file)) {
$csvfile = fopen($target_file , 'r');
$i = 0;
while (($data = fgetcsv($csvfile,",")) !== FALSE) {
if ($i > 0) {
$sql[] = '("'.($data[0]).'","'.$data[2].'","'.$data[1].'","'.$data[3].'","'.$data[4].'","'.$data[5].'","'.$data[6].'","'.$data[7].'","'.$data[8].'")';
}
$i++;
}
$query=mysqli_query($con,'INSERT INTO users (member_id, fname,lname,mname,sex,rating,expiration,last_played,email) VALUES '.implode(',', $sql).
' ON DUPLICATE KEY UPDATE member_id=VALUES(member_id),fname=VALUES(fname),lname=VALUES(lname),
mname=VALUES(mname),sex=VALUES(sex),rating=VALUES(rating),expiration=VALUES(expiration),last_played=VALUES(last_played),email=VALUES(email)');
if($query){
fclose($csvfile);
echo json_encode(array('error' => 'false', 'msg' => 'The file '.$newname . ' has been uploaded.'));
die();
}
else{
fclose($csvfile);
echo json_encode(array('error' => 'true', 'msg' => mysqli_error($con)));
die();
}
} else {
echo json_encode(array('error' => 'true', 'msg' => 'Sorry, there was an error uploading your file.'));
die();
}
} else {
echo json_encode(array('error' => 'true', 'msg' => 'Only CSV files are allowed.'));
die();
}
?>