How can SQL injection vulnerabilities be mitigated in PHP code that handles user input, such as GET parameters?
SQL injection vulnerabilities can be mitigated in PHP code by using prepared statements with parameterized queries. This approach separates the SQL query logic from the user input, preventing malicious SQL code from being executed. By binding user input as parameters in the query, the database engine treats them as data values rather than executable code.
// Example PHP code snippet using prepared statements to mitigate SQL injection vulnerabilities
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($stmt = $mysqli->prepare("SELECT * FROM users WHERE username = ? AND password = ?")) {
$stmt->bind_param("ss", $_GET['username'], $_GET['password']);
$stmt->execute();
// Process the results
$stmt->close();
}
$mysqli->close();
Related Questions
- How can one ensure that the HTML output generated from nested loops in PHP is valid and structured correctly?
- What advantages does using a database like PostgreSQL/PostGIS offer for optimizing distance calculations in PHP?
- Is it recommended to use JavaScript or PHP for interactive elements in a web form?