What is the best practice for transferring an ID from one table to another in PHP?

When transferring an ID from one table to another in PHP, the best practice is to first retrieve the ID from the source table, then insert it into the destination table. This ensures data integrity and prevents errors. It is important to properly sanitize and validate the ID before transferring it to avoid any security vulnerabilities.

// Retrieve ID from source table
$query = "SELECT id FROM source_table WHERE condition = 'value'";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
$id = $row['id'];

// Insert ID into destination table
$query = "INSERT INTO destination_table (id) VALUES ('$id')";
mysqli_query($connection, $query);