-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.php
76 lines (62 loc) · 2.1 KB
/
create.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
<?php
$failed = false;
if ($_SERVER["REQUEST_METHOD"] === "POST") {
if (empty($_POST["title"])) {
die("Note title cannot be null");
}
$mysqli = require __DIR__ . "/db.php";
var_dump($_POST);
$sql = sprintf(
"INSERT INTO notes (title, body, tag) VALUES ('%s','%s','%s')",
$mysqli->real_escape_string($_POST["title"]),
$mysqli->real_escape_string($_POST["body"]),
$mysqli->real_escape_string($_POST["tag"])
);
try {
$res = $mysqli->query($sql);
header("Location: index.php");
exit();
} catch (Exception $e) {
// die($mysqli->error . " " . $mysqli->errno);
$failed = true;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add a note</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
background-color: #ccd5ae;
}
input,
textarea,
button {
margin-bottom: 2rem;
}
</style>
</head>
<body>
<div class="mx-auto pt-5" style="width:500px">
<h1>Add a note</h1>
<?php if ($failed) : ?>
<h2>There was an error in adding the note</h2>
<?php endif; ?>
<form method="post">
<label for="title" class="form-label">Note title</label>
<input type="text" name="title" id="title" class="form-control" required>
<label for="body" class="form-label">Body (optional)</label>
<textarea name="body" id="body" cols="60" rows="8" class="form-control"></textarea>
<label for="tag" class="form-label">Note tag (optional)</label>
<input type="text" name="tag" id="tag" class="form-control">
<input type="submit" value="Create a note +" class="btn btn-primary">
</form>
<a href="index.php"><button class="btn btn-secondary">Cancel</button></a>
</div>
</body>
</html>