What are some best practices for updating a database table using PHP to increment a download count when a user clicks a download button?

One best practice for updating a database table using PHP to increment a download count when a user clicks a download button is to first establish a database connection, then retrieve the current download count from the database, increment it by one, and finally update the database with the new count.

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

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

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

// Retrieve current download count
$sql = "SELECT downloads FROM download_count";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$downloads = $row['downloads'];

// Increment download count
$downloads++;

// Update database with new count
$sql = "UPDATE download_count SET downloads = $downloads";
$conn->query($sql);

// Close database connection
$conn->close();
?>