-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost_edit.php
More file actions
92 lines (76 loc) · 2.77 KB
/
post_edit.php
File metadata and controls
92 lines (76 loc) · 2.77 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
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
session_start();
require "core/config.php";
if (!isset($_GET["id"])) {
header("location: index.php");
exit();
}
$post_query = $connection->query("SELECT * FROM post WHERE ID = '{$_GET['id']}'");
$post = mysqli_fetch_array($post_query);
if (!$post) {
header("location: index.php");
exit();
}
if (isset($_POST["edit-post"])) {
$author = $_POST["author"];
$title = $_POST["title"];
$body = $_POST["body"];
try {
$user_query = $connection->query("SELECT username FROM user WHERE username = '$author'");
$user = mysqli_fetch_array($user_query);
if ($user) {
$connection->query("UPDATE post SET author = '$author', title = '$title', body = '$body' WHERE ID = '{$_GET['id']}'");
$_SESSION["message"] = "Post was edited successfully";
$_SESSION["message-type"] = "primary";
header("location: index.php");
exit();
} else {
$_SESSION["message"] = "This author is not exists";
$_SESSION["message-type"] = "danger";
$URL = $URLS["post_edit"] . $_GET["id"];
header("location: $URL");
exit();
}
} catch (Exception $e) {
$_SESSION["message"] = "Something went wrong";
$_SESSION["message-type"] = "warning";
$URL = $URLS["post_edit"] . $_GET["id"];
header("location: $URL");
exit();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Post Edit</title>
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/style.css">
<style>
body {
background-color:rgb(213, 217, 220);
}
</style>
</head>
<body>
<div class="content">
<header>
<h2>PHP CRUD APPLICATION</h2>
<a href="index.php" class="btn btn-light w-100"><i>Back To Main Page</i></a>
</header>
<br>
<?php include 'includes/message.php' ?>
<div class="post-group">
<!-- Edit Post Form -->
<form action="" method="POST">
<input type="text" name="author" placeholder="Author" maxlength="200" class="form-control" value="<?php echo $post['author']; ?>" required>
<input type="text" name="title" placeholder="Title" maxlength="50" class="form-control" value="<?php echo $post['title']; ?>" required>
<textarea name="body" rows="5" placeholder="Body" class="form-control" required><?php echo $post['body']; ?></textarea>
<input type="submit" value="Edit Post" name="edit-post" class="btn btn-primary">
</form>
</div>
</div>
<script src="js/script.js"></script>
</body>