How can duplicate threads on the same topic be avoided in a PHP forum?

To avoid duplicate threads on the same topic in a PHP forum, you can implement a check when a new thread is created to see if a similar thread already exists. This can be done by comparing the title or content of the new thread with existing threads. If a similar thread is found, you can redirect the user to the existing thread instead of creating a duplicate.

// Check for duplicate threads before creating a new thread
$newThreadTitle = $_POST['title'];
$newThreadContent = $_POST['content'];

// Query to check if a similar thread already exists
$query = "SELECT * FROM threads WHERE title = '$newThreadTitle' OR content = '$newThreadContent'";
$result = mysqli_query($conn, $query);

if(mysqli_num_rows($result) > 0) {
    // Redirect user to existing thread
    $existingThread = mysqli_fetch_assoc($result);
    header("Location: thread.php?id=" . $existingThread['id']);
    exit();
} else {
    // Create new thread
    // Insert new thread into database
}