In the context of PHP and MySQL, why is it important to properly format string values, such as using single quotes around VARCHAR values in SQL queries?
When constructing SQL queries in PHP, it is important to properly format string values to prevent SQL injection attacks and ensure the query executes correctly. This includes using single quotes around VARCHAR values to indicate that they are strings. Failing to do so can lead to syntax errors or allow malicious users to manipulate the query.
// Example of properly formatting string values in a SQL query
$name = "John Doe";
$email = "johndoe@example.com";
// Using single quotes around string values in the SQL query
$query = "INSERT INTO users (name, email) VALUES ('$name', '$email')";
$result = mysqli_query($connection, $query);
if ($result) {
echo "Data inserted successfully";
} else {
echo "Error: " . mysqli_error($connection);
}