Online Guest Post

Here’s a PHP script for a simple guest post system that allows users to submit posts and view them. It includes a form for submitting posts and displays all posts from a MySQL database.

<?php
// Database connection settings
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "guestbook";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Handle form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $conn->real_escape_string($_POST['name']);
$message = $conn->real_escape_string($_POST['message']);

// Insert post into database
$sql = "INSERT INTO posts (name, message, created_at) VALUES ('$name', '$message', NOW())";

if ($conn->query($sql) === TRUE) {
echo "<p class='success'>Post submitted successfully!</p>";
} else {
echo "<p class='error'>Error: " . $sql . "<br>" . $conn->error . "</p>";
}
}

// Fetch all posts
$sql = "SELECT name, message, created_at FROM posts ORDER BY created_at DESC";
$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Guestbook</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.form-container {
margin-bottom: 20px;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
.form-container input, .form-container textarea {
width: 100%;
margin: 10px 0;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
}
.form-container input[type="submit"] {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
}
.form-container input[type="submit"]:hover {
background-color: #45a049;
}
.post {
border-bottom: 1px solid #eee;
padding: 10px 0;
margin-bottom: 10px;
}
.post h3 {
margin: 0;
color: #333;
}
.post p {
margin: 5px 0;
}
.success {
color: green;
font-weight: bold;
}
.error {
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<h1>Guestbook</h1>

<!-- Post submission form -->
<div class="form-container">
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>

<label for="message">Message:</label>
<textarea id="message" name="message" rows="4" required></textarea>

<input type="submit" value="Submit Post">
</form>
</div>

<!-- Display posts -->
<h2>Posts</h2>
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<div class='post'>";
echo "<h3>" . htmlspecialchars($row["name"]) . "</h3>";
echo "<p>" . htmlspecialchars($row["message"]) . "</p>";
echo "<small>Posted on: " . $row["created_at"] . "</small>";
echo "</div>";
}
} else {
echo "<p>No posts yet. Be the first to post!</p>";
}

$conn->close();
?>
</body>
</html>

To use this guestbook system, you’ll need to:

  1. Set up a MySQL database named “guestbook”
  2. Create a table named “posts” with the following SQL:
CREATE TABLE posts (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
message TEXT NOT NULL,
created_at DATETIME NOT NULL
);

3. Update the database connection settings ($servername, $username, $password, $dbname) in the PHP script to match your MySQL configuration.


The script includes:

  • A form for submitting posts with name and message fields
  • Basic CSS styling for a clean layout
  • Database connection and query handling
  • Security measures like escaping input to prevent SQL injection
  • Display of all posts in descending order by date
  • Success/error messages for form submission

Place the PHP file in your web server’s document root and ensure your server has PHP and MySQL installed.

By Admin

Leave a Reply

Your email address will not be published. Required fields are marked *