-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit_blog.php
81 lines (67 loc) · 2.59 KB
/
submit_blog.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
77
78
79
80
81
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="submitblog.css">
<title>Writing a blog</title>
</head>
<body>
<?php
require_once('header.php');
?>
<br><br><br>
<div class="form-block">
<form action = "submit_blog.php" method = "post">
<p><label for="blog_topic">Blog Topic :</label>
<input type="text" name="blog_topic" id="blog_topic" size = "44" /></p>
<p><label for="topic_of_interest">Author name, a short text :</label>
<input type="text" name="topic_of_interest" id="topic_of_interest" size="44" /></p>
<p><label for="content">Content :</label>
<textarea name="content" id="content"></textarea></p>
<p><input type = "submit" name="submit"/></p>
</form>
<!-- writing the data to the database, if form is submitted -->
<?php
if (isset($_POST['submit'])) {
if (strlen($_POST['blog_topic']) != 0 && strlen($_POST['content']) != 0) {
//reading the form entries
$blog_topic = $_POST['blog_topic'];
// replacing the ' with \'
$blog_topic = str_replace("'","\'",$blog_topic);
$topic_of_interest = $_POST['topic_of_interest'];
// replacing the ' with \'
$topic_of_interest = str_replace("'","\'",$topic_of_interest);
$content = $_POST['content'];
// replacing the ' with \'
$content = str_replace("'","\'",$content);
$date = date("F j, Y");
//making connection with the database
$servername = "localhost";
$username = "root";
$password = "";
$database = "blogs";
$conn = mysqli_connect($servername, $username, $password, $database);
if ($conn) {
// writing entries to the blogs database
$sql = "INSERT INTO `blogs` (`date`, `blog_topic`, `topic_of_interest`, `content`) VALUES ('$date', '$blog_topic', '$topic_of_interest', '$content')";
$result = mysqli_query($conn, $sql);
if ($result) {
echo "your blog is saved in the database and will be displayed on the website.<br>";
}
else {
echo "your blog failed to enter the database, we regret for the inconvinience.<br>";
}
}
else {
die("connection with blogs database was unsuccessful : ".mysqli_connect_error());
}}
}
?>
</div>
<?php
require_once('footer.php');
?>
</body>
</html>