-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscribe.php
78 lines (70 loc) · 2.22 KB
/
subscribe.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
<?php
/******************************************************************
* Constants
******************************************************************/
define('DATABASE_DIRECTORY', 'db');
define('DATABASE_NAME', 'sample');
define('DATABASE_MODE', 0666);
/******************************************************************
* Variables
******************************************************************/
$full_path = sprintf('%s/%s.db', DATABASE_DIRECTORY, DATABASE_NAME);
$handler = new PDO('sqlite:' . $full_path);
/******************************************************************
* SQL
******************************************************************/
$sql_create_table = '
CREATE TABLE "subscribers" (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"email" text(255) NOT NULL
);
';
$sql_insert_email = '
INSERT INTO subscribers (email) VALUES (:email);
';
/******************************************************************
* Create table
******************************************************************/
try {
$handler->exec($sql_create_table);
} catch (PDOException $e) {
echo json_encode(array(
'error' => TRUE,
'message' => $e->getMessage(),
));
die;
}
/******************************************************************
* Insert email
******************************************************************/
if (!empty($_POST['subscribe'])) {
if (filter_var($_POST['subscribe'], FILTER_VALIDATE_EMAIL)) {
$statement = $handler->prepare($sql_insert_email);
if ($statement) {
$statement->bindValue(':email', $_POST['subscribe'], SQLITE3_TEXT);
$statement->execute();
echo json_encode(array(
'message' => 'OK',
));
die;
} else {
echo json_encode(array(
'error' => TRUE,
'message' => 'Can not insert an email.',
));
die;
}
} else {
echo json_encode(array(
'error' => TRUE,
'message' => 'E-mail has wrong format.',
));
die;
}
} else {
echo json_encode(array(
'error' => TRUE,
'message' => 'E-mail is missing.',
));
die;
}