-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave_blogs.php
More file actions
65 lines (50 loc) · 1.81 KB
/
save_blogs.php
File metadata and controls
65 lines (50 loc) · 1.81 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
<?php
// Establish a database connection (replace with your actual database credentials)
$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "user_databse";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check the connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Your code to save blogs should come after the database connection is established
// Your database connection code
// ... your code ...
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get the username of the currently logged-in user
session_start();
// Check if the user is authenticated
if (!isset($_SESSION['username'])) {
// Redirect to the login page or display an error message
header('Location: login.html');
exit();
}
else if (isset($_SESSION['username'])) {
$username = $_SESSION['username'];
}
// Your database connection code (already set up)
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get the username of the currently logged-in user
$username = $_SESSION['username'];
// Extract blog input
$title = $_POST['title'];
$content = $_POST['content'];
// Your database connection code (already set up)
// Insert the blog data into the "blog_data" table
$insert_query = "INSERT INTO blogs (username, title, content) VALUES ('$username', '$title', '$content')";
if ($conn->query($insert_query) === TRUE) {
// Blog data inserted successfully
header('Location: blogs.php');
exit();
} else {
// Handle the error
echo "Error: " . $insert_query . "<br>" . $conn->error;
}
}
// Close the database connection
session_destroy();
$conn->close();
}
?>