Consider this PHP code snippet that builds an SQL query unsafely using user input. What will be the output when the input is admin' -- ?
<?php $user_input = "admin' -- "; $query = "SELECT * FROM users WHERE username = '$user_input'"; echo $query; ?>
Look carefully at how the input is inserted directly into the query string.
The input admin' -- closes the string early and comments out the rest of the query. The output shows the query with the input inserted as-is, causing a potential SQL injection.
Given this PHP code snippet, which part is the main cause of SQL injection vulnerability?
$query = "SELECT * FROM users WHERE username = '" . $_GET['user'] . "'";
Think about how user input is handled inside the query.
Directly adding user input into the query string without validation or escaping allows attackers to inject malicious SQL code.
Look at this PHP code snippet. What error or behavior will happen if the input is ' OR 1=1 -- ?
$input = "' OR 1=1 -- "; $sql = "SELECT * FROM users WHERE username = '$input'"; $result = mysqli_query($conn, $sql);
Consider what the injected SQL code does inside the WHERE clause.
The input closes the username string and adds OR 1=1, which is always true, so the query returns all rows, bypassing authentication.
Given this PHP code snippet, which option will cause a syntax error when executed?
$input = $_GET['input']; $sql = "SELECT * FROM users WHERE username = '$input'";
Look at how quotes are used and closed in the query string.
Option A introduces a semicolon, attempting to execute multiple SQL statements (DROP TABLE users), which causes a syntax error since the query expects a single statement.
Given this PHP login code snippet, how can an attacker craft the username input to bypass password verification?
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo 'Login successful';
} else {
echo 'Login failed';
}Think about how to make the WHERE clause always true by injecting SQL in username.
Option C injects ' OR '1'='1 in username, making the WHERE clause always true and bypassing password check.