How can you optimize the PHP script to accurately track and manage player suspensions based on red card incidents in a game?

To optimize the PHP script for tracking and managing player suspensions based on red card incidents, you can create a database table to store player information, including the number of red cards received. When a red card is issued, increment the red card count for the respective player in the database. Then, you can query the database to check if a player has received multiple red cards and apply suspension rules accordingly.

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "players";

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

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

// Increment red card count for player
$player_id = 1; // Example player ID
$sql = "UPDATE players SET red_cards = red_cards + 1 WHERE id = $player_id";
$conn->query($sql);

// Check if player has received multiple red cards
$sql = "SELECT red_cards FROM players WHERE id = $player_id";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    if ($row["red_cards"] >= 2) {
        echo "Player suspended for multiple red cards.";
        // Apply suspension rules here
    }
}

$conn->close();