What are the potential pitfalls of using DELETE and WHERE statements in PHP to delete data from a database?
Potential pitfalls of using DELETE and WHERE statements in PHP to delete data from a database include the risk of SQL injection attacks if user input is not properly sanitized. To mitigate this risk, it is important to use prepared statements with parameterized queries to safely delete data based on specific conditions.
// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
// Prepare a DELETE statement with a WHERE clause
$stmt = $pdo->prepare("DELETE FROM mytable WHERE id = :id");
// Bind the parameter to the statement
$stmt->bindParam(':id', $id);
// Execute the statement
$stmt->execute();