Skip to content
PHP Logo

How can SQL injection vulnerabilities be prevented when updating database entries using PHP?

SQL injection vulnerabilities can be prevented when updating database entries using PHP by using prepared statements with parameterized queries. This method ensures that user input is treated as data rather than executable code, thereby preventing malicious SQL injection attacks.

php
// Establish a connection to the database
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare a SQL statement with placeholders
$stmt = $pdo->prepare("UPDATE mytable SET column1 = :value1 WHERE id = :id");

// Bind the parameters to the placeholders
$stmt->bindParam(':value1', $value1);
$stmt->bindParam(':id', $id);

// Execute the statement with the sanitized input
$stmt->execute();