-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_post.php
29 lines (28 loc) · 975 Bytes
/
create_post.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
<?php
session_start();
if($_POST['title'] != "" && $_POST['body'] != "") {
$title = $_POST['title'];
$body = $_POST['body'];
$conn = "mysql:host=127.0.0.1;port=8889;dbname=meatlabs";
try {
$db = new PDO($conn, "root", "root", [
PDO::ATTR_PERSISTENT => true
]);
$stmt = $db->prepare("INSERT INTO posts (user_id, title, body) VALUES (?,?,?)");
if($stmt->execute([$_SESSION['id'], $_POST['title'], $_POST['body']])) {
$_SESSION['SUCCESS_MESSAGE'] = 'Post successfully created.';
header('Location: homepage.php');
}
else {
die("Unable to fetch data.");
}
}
catch(PDOException $e) {
die("Could not connect: " . $e->getMessage());
}
}
else {
$_SESSION['ERROR'] = 'There are empty fields.';
header('Location: homepage.php');
}
?>