What are some potential solutions to prevent duplicate entries in a PHP guestbook form?
One potential solution to prevent duplicate entries in a PHP guestbook form is to check if the submitted entry already exists in the database before inserting it. This can be done by querying the database for entries with the same content. If a matching entry is found, the form submission can be rejected.
// Connect to the database
$connection = new mysqli('localhost', 'username', 'password', 'database');
// Check if the form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$entry = $_POST['entry'];
// Check if the entry already exists in the database
$query = "SELECT * FROM guestbook WHERE entry = '$entry'";
$result = $connection->query($query);
if ($result->num_rows > 0) {
echo "Entry already exists in the guestbook.";
} else {
// Insert the new entry into the database
$insertQuery = "INSERT INTO guestbook (entry) VALUES ('$entry')";
$connection->query($insertQuery);
echo "Entry added to the guestbook.";
}
}
// Close the database connection
$connection->close();