What potential security risks are present in the PHP code provided for a guestbook?
The potential security risks present in the PHP code for the guestbook include the lack of input validation and sanitization, which can make the application vulnerable to SQL injection attacks. To solve this issue, input data should be properly validated and sanitized before being used in SQL queries to prevent malicious code execution.
// Validate and sanitize input data before using it in SQL queries
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
// Prepare and execute SQL query with sanitized input data
$stmt = $pdo->prepare("INSERT INTO guestbook (name, message) VALUES (:name, :message)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':message', $message);
$stmt->execute();