How can PHP be utilized to create a link redirection script for tracking link clicks in a table?

To create a link redirection script for tracking link clicks in a table, you can use PHP to increment a counter in a database each time a link is clicked. You can then redirect the user to the desired link after updating the click count.

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

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

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

// Get the link ID from the URL
$link_id = $_GET['link_id'];

// Update the click count for the link
$sql = "UPDATE links SET click_count = click_count + 1 WHERE id = $link_id";
$conn->query($sql);

// Get the destination URL for the link
$sql = "SELECT url FROM links WHERE id = $link_id";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$destination_url = $row['url'];

// Redirect the user to the destination URL
header("Location: $destination_url");
exit;
?>