What are the advantages of using a database over file storage for managing guestbook entries in PHP?
Using a database to manage guestbook entries in PHP offers several advantages over file storage. Databases provide better security by allowing for user authentication and access control. They also offer more efficient data retrieval and manipulation through SQL queries. Additionally, databases allow for easier scalability and data organization compared to storing entries in individual files.
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "guestbook";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Insert a new entry into the guestbook table
$name = "John Doe";
$message = "Hello, world!";
$sql = "INSERT INTO guestbook (name, message) VALUES ('$name', '$message')";
if ($conn->query($sql) === TRUE) {
echo "New entry added successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// Close the connection
$conn->close();