How can you securely handle user input in an edit function to prevent SQL injection attacks in PHP?

To securely handle user input in an edit function to prevent SQL injection attacks in PHP, you should use prepared statements with parameterized queries. This approach separates the SQL query from the user input, preventing malicious SQL code from being injected into the query.

// Assuming $conn is your database connection

// Get user input from a form
$user_input = $_POST['user_input'];

// Prepare a SQL statement with a parameterized query
$stmt = $conn->prepare("UPDATE table_name SET column_name = ? WHERE id = ?");
$stmt->bind_param("si", $user_input, $id);

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

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