How can the PHP script be modified to ensure that the data retrieved from the database corresponds to the variable passed in the URL?
To ensure that the data retrieved from the database corresponds to the variable passed in the URL, you can use prepared statements with placeholders for the variable in the SQL query. This helps prevent SQL injection attacks and ensures that the data retrieved is based on the variable passed in the URL.
<?php
// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Retrieve the variable from the URL
$id = $_GET['id'];
// Prepare a SQL statement with a placeholder for the variable
$stmt = $pdo->prepare('SELECT * FROM your_table WHERE id = :id');
$stmt->bindParam(':id', $id);
$stmt->execute();
// Fetch the data
$data = $stmt->fetch(PDO::FETCH_ASSOC);
// Use the retrieved data as needed
echo $data['column_name'];
?>