-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch-insert-cards.php
More file actions
43 lines (29 loc) · 1.17 KB
/
batch-insert-cards.php
File metadata and controls
43 lines (29 loc) · 1.17 KB
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
$pdo = new PDO('mysql:dbname=redqueen', 'root');
$file = fopen("php://stdin", "r");
$header = fgets($file);
$queries =<<<SQL
INSERT INTO `cards` (name,code,pin,isActive,created_at,updated_at) VALUES (?, ?, ?, true, NOW(), NOW());
INSERT INTO `card_schedule` (card_id, schedule_id) VALUES (LAST_INSERT_ID(), ?);
SQL;
$createStatement = $pdo->prepare($queries);
$selectStatement = $pdo->prepare('SELECT id FROM cards WHERE code = ?');
while ($fields = fgetcsv($file)) {
$fields = array_map('trim', $fields);
list($name, $pin, $code, $schedule_id) = $fields;
list($last_name, $first_name) = explode(', ', $name, 2);
echo "name= $first_name $last_name code=$code";
$fullCode = strtoupper(dechex(21).dechex($code));
$selectStatement->execute([$fullCode]);
if (count($selectStatement->fetchAll()) > 0) {
echo " success=0 err=existing" . PHP_EOL;
} else {
$success = $createStatement->execute([implode(' ', [$first_name, $last_name]), $fullCode, $pin, $schedule_id]);
echo " success=$success" . PHP_EOL;
$createStatement->closeCursor();
if (!$success) {
var_dump($createStatement->errorInfo());
}
}
$selectStatement->closeCursor();
}