What are the best practices for escaping values before inserting them into a database using PHP?

When inserting values into a database using PHP, it is important to escape the values to prevent SQL injection attacks. The best practice is to use prepared statements with parameterized queries, which automatically handle escaping and sanitizing input data. This method ensures that user input is treated as data rather than executable code, making the database more secure.

// Example of inserting values into a database using prepared statements
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

$stmt = $pdo->prepare("INSERT INTO my_table (column1, column2) VALUES (:value1, :value2)");
$stmt->bindParam(':value1', $value1);
$stmt->bindParam(':value2', $value2);

$value1 = $_POST['value1']; // Assuming this is user input
$value2 = $_POST['value2']; // Assuming this is user input

$stmt->execute();