-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.php
51 lines (41 loc) · 1.46 KB
/
upload.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
44
45
46
47
48
49
50
51
<?php
require __DIR__ . "/vendor/autoload.php";
require "Database.php";
use Fusonic\CsvReader\Attributes\TitleMapping;
use Fusonic\CsvReader\CsvReader;
class Entity
{
#[TitleMapping('firstname')]
public string $firstname;
#[TitleMapping('lastname')]
public string $lastname;
#[TitleMapping('email')]
public string $email;
#[TitleMapping('phone')]
public string $phone;
}
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$database = new Database();
$reader = new CsvReader($_FILES['file']['tmp_name']);
try {
foreach ($reader->readObjects(Entity::class) as $item) {
$database->insert('INSERT INTO employees (firstname, lastname, email, phone) VALUES(:firstname, :lastname, :email, :phone)', [
'firstname' => $item->firstname,
'lastname' => $item->lastname,
'email' => $item->email,
'phone' => $item->phone,
]);
}
$database->insert('INSERT INTO campaigns(name, created_at) VALUES(:name, :created_at)', [
'name' => $_POST['campaign-name'],
'created_at' => $_POST['campaign-date'],
]);
$message = urlencode('Data has been imported successfully.');
header('Location: index.php?message=' . $message);
} catch (Exception $exception) {
$message = urlencode("Error: " . $exception->getMessage());
header('Location: index.php?message=' . $message);
exit();
}
}