forked from if-itb/IF3110-01-Simple-Blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_table.php
More file actions
57 lines (48 loc) · 1.93 KB
/
update_table.php
File metadata and controls
57 lines (48 loc) · 1.93 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
<?php
if (isset($_POST['ID_post']) && isset($_POST['Judul']) &&
isset($_POST['Tanggal']) && isset($_POST['Konten'])) {
$id = $_POST['ID_post'];
$title = $_POST['Judul'];
$postdate = date('Y-m-d', strtotime($_POST['Tanggal']));
$content = $_POST['Konten'];
$isFeatured = isset($_POST['isFeatured']) ? 1 : 0;
} else {
echo "Failed, wrong POST";
die();
}
$mysqli = new mysqli("localhost", "WBD_USER", "QKC3zwhJ", "WBD_DB");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
die();
}
if ($id == "null") { // ADD TO DATABASE
if (!($stmt = $mysqli->prepare("INSERT INTO `post`(`TITLE`, `POSTDATE`, `CONTENT`, `ISFEATURED`) VALUES (?, ?, ?, ?)"))) {
echo "Prepare failed: (" . $mysqli->errno . ") " > $mysqli->error;
die();
}
if (!$stmt->bind_param("sssi", $title, $postdate, $content, $isFeatured)) {
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
die();
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
die();
}
} else {
$id = +$id;
if (!($stmt = $mysqli->prepare("UPDATE `post` SET `TITLE`=?, `POSTDATE`=?, `CONTENT`=?, `ISFEATURED`=? WHERE `ID`=?"))) {
echo "Prepare failed: (" . $mysqli->errno . ") " > $mysqli->error;
die();
}
if (!$stmt->bind_param("sssii", $title, $postdate, $content, $isFeatured, $id)) {
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
die();
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
die();
}
}
$relative = "index.php";
header("Location: ".$relative);
?>