How can you use PHP to perform database queries to display race details based on user selection?

To display race details based on user selection, you can use PHP to perform database queries using SQL statements. First, you need to establish a connection to your database, then retrieve the user's selection (such as a race ID) from a form submission or URL parameter. Next, you can use a SELECT query to fetch the race details from the database based on the user's selection. Finally, you can display the retrieved race details on your webpage.

<?php
// Establish a connection to your database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "races_database";

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

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

// Retrieve user's selection (e.g., race ID)
$race_id = $_GET['race_id'];

// Perform a database query to fetch race details based on user selection
$sql = "SELECT * FROM races WHERE id = $race_id";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Race Name: " . $row["race_name"]. "<br>";
        echo "Location: " . $row["location"]. "<br>";
        echo "Date: " . $row["date"]. "<br>";
        // Add more details as needed
    }
} else {
    echo "No race details found for the selected race ID.";
}

$conn->close();
?>