What are the best practices for passing and retrieving IDs in PHP when editing database entries?

When editing database entries in PHP, it is important to securely pass and retrieve IDs to ensure data integrity and prevent SQL injection attacks. One best practice is to use prepared statements with placeholders to handle user input safely. Additionally, using sessions or hidden form fields to pass IDs between pages can help maintain the state of the application.

// Example of passing and retrieving IDs securely in PHP

// Pass ID securely using a session variable
session_start();
$_SESSION['id'] = $id;

// Retrieve ID securely using prepared statements
$stmt = $pdo->prepare("SELECT * FROM table WHERE id = :id");
$stmt->bindParam(':id', $_SESSION['id']);
$stmt->execute();
$row = $stmt->fetch();