When using variables in DELETE FROM queries in PHP, what precautions should be taken to ensure correct execution?
When using variables in DELETE FROM queries in PHP, it is important to sanitize the input to prevent SQL injection attacks. This can be done by using prepared statements with placeholders for the variables, which will automatically escape the values and prevent malicious code from being injected into the query.
// Assuming $conn is your database connection
// Sanitize the input
$id = mysqli_real_escape_string($conn, $_POST['id']);
// Prepare the statement
$stmt = $conn->prepare("DELETE FROM table_name WHERE id = ?");
$stmt->bind_param("i", $id);
// Execute the statement
$stmt->execute();
// Close the statement and connection
$stmt->close();
$conn->close();
Related questions
-
user agent browser detection Internet Explorer
-
performance loop variable
-
PHPMailer SwiftMailer mail() function