-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrage_to_db.php
More file actions
72 lines (53 loc) · 2.04 KB
/
migrage_to_db.php
File metadata and controls
72 lines (53 loc) · 2.04 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/**
* Please use command.sql to build tables for your database, name it `guitest`.
* Please insert the password for the root of your DB below.
*/
$password = 'ROOT_PASSWORD';
require_once('bootstrap.php'); // Reuse the code
function date_string_to_unix($date) {
$dateInfo = date_parse_from_format('m-j-Y G:i:s', $date);
$unixTimestamp = mktime(
$dateInfo['hour'], $dateInfo['minute'], $dateInfo['second'],
$dateInfo['month'], $dateInfo['day'], $dateInfo['year']
);
return $unixTimestamp;
}
$file_contents = file_get_contents('./form_submissions.txt', true);
// split by newlines
$lines = explode("\n", $file_contents);
$db = new PDO("mysql:host=localhost;dbname=guitest", 'root', $password);
/* * * set the PDO error mode to exception ** */
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
foreach ($lines as $line) {
// skip on sanity checks
if (trim($line) == '') {
continue;
}
try {
$db->beginTransaction();
$message_values = explode(',', $line);
// decode
foreach ($message_values as &$value) {
$value = htmlspecialchars(FileStorage::decode(trim($value)), ENT_QUOTES);
}
$query = "INSERT INTO customer (name) VALUES ('{$message_values[1]}')";
print $query."\n";
$db->exec($query);
$cusomer_last_id = $db->lastInsertId();
$query = "INSERT INTO recipient (name) VALUES ('{$message_values[2]}')";
print $query."\n";
$db->exec($query);
$recipient_last_id = $db->lastInsertId();
$unix_date = date_string_to_unix($message_values[0]);
$query = "INSERT INTO message (customer_id, recipient_id, message, received)
VALUES ('$cusomer_last_id', '$recipient_last_id', '{$message_values[3]}', '$unix_date')";
print $query."\n";
$db->exec($query);
$db->commit();
}
catch (PDOException $e) {
echo "Rolling back... ".$e->getMessage() . "\n";
$db->rollback();
}
}