What are the potential risks of not properly escaping user input in PHP when interacting with a database?
The potential risks of not properly escaping user input in PHP when interacting with a database include SQL injection attacks, where malicious SQL code is inserted into input fields to manipulate the database. To prevent this, always sanitize and escape user input before using it in database queries.
// Example of properly escaping user input in PHP using mysqli_real_escape_string
// Assume $conn is the database connection
$input = $_POST['user_input'];
$escaped_input = mysqli_real_escape_string($conn, $input);
// Use $escaped_input in your database query
$query = "SELECT * FROM users WHERE username='$escaped_input'";
$result = mysqli_query($conn, $query);