How can PHP be used to update a counter in a table when a link is clicked?

To update a counter in a table when a link is clicked, you can create a PHP script that increments the counter value in the database table each time the link is clicked. This can be achieved by sending an AJAX request to the PHP script when the link is clicked, which will then update the counter value in the database.

<?php
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";

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

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

// Update counter in database
if(isset($_GET['link_clicked'])){
    $sql = "UPDATE counter_table SET counter = counter + 1 WHERE id = 1";
    $conn->query($sql);
}

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

In your HTML file, you can call this PHP script using AJAX when the link is clicked:

```html
<!DOCTYPE html>
<html>
<head>
    <title>Update Counter</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
    <a href="#" id="updateCounter">Click me to update counter</a>

    <script>
    $(document).ready(function(){
        $('#updateCounter').click(function(e){
            e.preventDefault();
            $.ajax({
                url: 'update_counter.php?link_clicked=true',
                type: 'GET',
                success: function(response){
                    console.log('Counter updated successfully');
                },
                error: function(){
                    console.log('Error updating counter');
                }
            });
        });
    });
    </script>
</body>
</html>