Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 76f5ad8

Browse files
committed
Finalises example
1 parent bbdc363 commit 76f5ad8

File tree

2 files changed

+56
-25
lines changed

2 files changed

+56
-25
lines changed

‎action.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
if ( ! is_null($post_action)) {
66
switch ($post_action) {
7-
case 'add_email':
7+
case 'add':
88
$address = isset($_POST['email']) && ! empty($_POST['email']) ? $_POST['email'] : null;
99
if ( ! is_null($address)) {
1010
add_email($address);
@@ -13,7 +13,7 @@
1313
}
1414
break;
1515
default:
16-
show_index();
16+
show_index('unknown action');
1717
break;
1818
}
1919
} else {
@@ -23,23 +23,34 @@
2323

2424
/// functions
2525

26-
function add_email() {
27-
// connect to database (NOTE: configure as needed)
28-
$host = 'localhost';
29-
$db = 'php-db-form';
30-
$user = 'root';
31-
$pass = 'root';
32-
33-
$pdo = new PDO("pgsql:host=$host;port=8889;dbname=$db;user=$user;password=$pass");
26+
function add_email($address) {
27+
require_once 'config.php';
28+
29+
// connect to mysql database
30+
try {
31+
$dns = 'mysql:host=' . HOST . ';dbname=' . DB;
32+
$pdo = new PDO($dns, USER, PASS);
33+
// set the PDO error mode to exception
34+
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
35+
} catch(PDOException $e) {
36+
show_index('mysql connection failed:' . $e->getMessage());
37+
}
3438

39+
// insert into database
40+
$timestamp = time();
3541
$stmt = $pdo->prepare('INSERT INTO emails (address, created_at) VALUES (:address, :created_at)');
36-
// $stmt->bindParam(':address', );
42+
$stmt->bindParam(':address', $address, PDO::PARAM_STR);
43+
$stmt->bindParam(':created_at', $timestamp, PDO::PARAM_INT);
44+
$inserted = $stmt->execute();
3745

38-
// create prepared statement, save to db
39-
40-
show_index();
46+
if ($inserted) {
47+
show_index();
48+
} else {
49+
show_index('something went wrong, please try again!');
50+
}
4151
}
4252

53+
4354
function show_index($msg = null) {
4455
if ( is_null($msg)) {
4556
header('Location: index.php');

‎index.php

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
<?php
22

3-
// connect to database
3+
require_once 'config.php';
4+
5+
// connect to mysql database
6+
try {
7+
$dns = 'mysql:host=' . HOST . ';dbname=' . DB;
8+
$pdo = new PDO($dns, USER, PASS);
9+
// set the PDO error mode to exception
10+
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
11+
} catch(PDOException $e) {
12+
echo '<p style="color:red">mysql connection failed:' . $e->getMessage() . '</p>';
13+
echo '<p>try to refresh</p>';
14+
}
415

516
// select data from database
17+
$stmt = $pdo->prepare('SELECT * from emails ORDER BY created_at DESC');
18+
$stmt->execute();
619

7-
$data = ['some@email.com', 'office@email.org'];
20+
$emails = $stmt->fetchAll(PDO::FETCH_ASSOC);
821

922
$error = isset($_GET['error']) && ! empty($_GET['error']) ? $_GET['error'] : null;
1023

@@ -16,17 +29,22 @@
1629
<title>Database Form</title>
1730

1831
<style>
19-
.DBForm {
32+
.DBForm,
33+
.Fields {
2034
border: 2px solid #222;
2135
border-radius: 2px;
36+
}
37+
38+
.DBForm {
2239
padding: 1rem;
2340
}
2441
.DBForm .input {
2542
width: 222px;
2643
}
27-
.DBForm .error {
44+
45+
.Error {
2846
color: tomato;
29-
padding-top: 0.4rem;
47+
margin-top: 0.6rem;
3048
}
3149

3250
.Fields .field {
@@ -50,20 +68,22 @@ class="input"
5068
/>
5169
<input
5270
type="submit"
53-
name="add_email"
71+
name="action"
5472
value="add"
5573
/>
56-
<div class="error">
57-
<?php echo $error; ?>
58-
</div>
5974
</form>
6075

76+
<div class="Error">
77+
<?php echo 'error: ' . $error; ?>
78+
</div>
79+
6180
<h2>Email addresses</h2>
6281
<div class="Fields">
6382
<?php
64-
foreach ($data as $key => $email) {
83+
foreach ($emails as $key => $email) {
6584
?><div class="field"><?php
66-
echo $key . '' . $email;
85+
$created_date = date('d.m.Y', $email['created_at']);
86+
echo $created_date . '' . $email['address'];
6787
?></div><?php
6888
}
6989
?>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /