Skip to content
PHP Logo

How can PHP developers prevent SQL injection vulnerabilities when executing DELETE queries in their applications?

To prevent SQL injection vulnerabilities when executing DELETE queries in PHP applications, developers should use prepared statements with parameterized queries. This approach ensures that user input is properly sanitized and treated as data rather than executable SQL code.

php
// Establish a database connection
$mysqli = new mysqli("localhost", "username", "password", "database");

// Prepare a DELETE query using a parameterized statement
$stmt = $mysqli->prepare("DELETE FROM users WHERE id = ?");

// Bind the user input to the query parameters
$id = $_POST['id']; // Assuming the user input is coming from a form
$stmt->bind_param("i", $id);

// Execute the query
$stmt->execute();

// Close the statement and connection
$stmt->close();
$mysqli->close();