How can a link click be saved in a database using PHP?

To save a link click in a database using PHP, you can create a table in your database to store the click data, including the link URL, timestamp, and any other relevant information. Then, you can use PHP to insert a new record into this table whenever a user clicks on a link.

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

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

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

// Get the link URL from the clicked link
$link_url = $_GET['link'];

// Insert the click data into the database
$sql = "INSERT INTO link_clicks (link_url, click_timestamp) VALUES ('$link_url', NOW())";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>