Complete the code to get the user input safely.
<?php
$username = $_GET[[1]];
?>The user input is accessed via the $_GET superglobal with the key as a string, so it must be quoted.
Complete the code to build an unsafe SQL query using the user input.
<?php $query = "SELECT * FROM users WHERE username = '[1]'"; ?>
The variable $username should be inserted directly inside the string with single quotes around it in the SQL query.
Fix the error in the unsafe query that allows SQL injection.
<?php $query = "SELECT * FROM users WHERE username = '" . [1] . "'"; ?>
htmlspecialchars which is for HTML, not SQL.Using mysqli_real_escape_string escapes special characters to prevent SQL injection.
Fill both blanks to prepare and execute a safe SQL statement.
<?php $stmt = $conn->prepare("SELECT * FROM users WHERE username = [1]"); $stmt->bind_param([2], $username); $stmt->execute(); ?>
The placeholder in the SQL statement is a question mark ?. The bind_param type for a string is s without quotes.
Fill all three blanks to fetch and display safe query results.
<?php $stmt = $conn->prepare("SELECT email FROM users WHERE username = [1]"); $stmt->bind_param([2], $username); $stmt->bind_result([3]); $stmt->execute(); $stmt->fetch(); echo $email; ?>
The placeholder is ?, the bind_param type is s, and the variable to bind the result is $email.