How can escaping input data before inserting it into a database prevent potential security vulnerabilities?

Escaping input data before inserting it into a database is crucial in preventing SQL injection attacks. By escaping input data, special characters are converted into their safe SQL representation, ensuring that malicious SQL queries cannot be executed. This helps to protect the database from potential security vulnerabilities.

// Assuming $conn is the database connection object

// Escape input data before inserting into the database
$user_input = mysqli_real_escape_string($conn, $_POST['user_input']);

// Insert escaped input data into the database
$sql = "INSERT INTO table_name (column_name) VALUES ('$user_input')";
mysqli_query($conn, $sql);