How can I prevent duplicate threads on the same issue in a PHP forum?

Issue: To prevent duplicate threads on the same issue in a PHP forum, you can check if a thread with the same topic already exists before allowing a new thread to be created. Code snippet:

// Assume $topic is the topic of the new thread being created

// Check if a thread with the same topic already exists
$query = "SELECT * FROM threads WHERE topic = '$topic'";
$result = mysqli_query($connection, $query);

if(mysqli_num_rows($result) > 0) {
    // Thread with the same topic already exists, prevent creating a duplicate
    echo "Thread with the same topic already exists.";
} else {
    // Create a new thread with the given topic
    // Add your code here to insert the new thread into the database
}