-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.php
92 lines (80 loc) · 1.8 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
require_once 'config.php';
$error = isset($_GET['error']) && ! empty($_GET['error']) ? $_GET['error'] : null;
// connect to mysql database
try {
$dns = 'mysql:host=' . HOST . ';dbname=' . DB;
$pdo = new PDO($dns, USER, PASS);
// set the PDO error mode to exception
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo '<p style="color:red">mysql connection failed: ' . $e->getMessage() . '</p>';
echo '<p>try to refresh</p>';
}
// select data from database
$stmt = $pdo->prepare('SELECT * from emails ORDER BY created_at DESC');
$stmt->execute();
$emails = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Database Form</title>
<style>
.DBForm,
.Fields {
border: 2px solid #222;
border-radius: 2px;
}
.DBForm {
padding: 1rem;
}
.DBForm .input {
width: 222px;
}
.Error {
color: tomato;
margin-top: 0.6rem;
}
.Fields .field {
border-bottom: 1px solid #222;
padding: 1rem;
}
</style>
</head>
<body>
<h1>Database Form</h1>
<form
method="post"
action="action.php"
class="DBForm"
>
<input
type="email"
name="email"
placeholder="Type your email address"
class="input"
/>
<input
type="submit"
name="action"
value="add"
/>
</form>
<div class="Error">
<?php if ( ! is_null($error)) echo 'error: ' . $error; ?>
</div>
<h2>Email addresses</h2>
<div class="Fields">
<?php
foreach ($emails as $key => $email) {
?><div class="field"><?php
$created_date = date('d.m.Y', $email['created_at']);
echo $created_date . ' – ' . $email['address'];
?></div><?php
}
?>
</div>
</body>
</html>