How can PHP be used to store user-selected security questions and answers in a database?
To store user-selected security questions and answers in a database using PHP, you can create a form where users can select their security questions and input their answers. Once the form is submitted, you can use PHP to validate and sanitize the input data before storing it in the database.
<?php
// Assuming you have already established a database connection
// Retrieve user-selected security questions and answers from the form
$question1 = $_POST['question1'];
$answer1 = $_POST['answer1'];
$question2 = $_POST['question2'];
$answer2 = $_POST['answer2'];
// Validate and sanitize the input data before storing in the database
$question1 = mysqli_real_escape_string($connection, $question1);
$answer1 = mysqli_real_escape_string($connection, $answer1);
$question2 = mysqli_real_escape_string($connection, $question2);
$answer2 = mysqli_real_escape_string($connection, $answer2);
// Insert the user-selected security questions and answers into the database
$query = "INSERT INTO security_questions (question, answer) VALUES ('$question1', '$answer1'), ('$question2', '$answer2')";
$result = mysqli_query($connection, $query);
if($result) {
echo "Security questions and answers have been successfully stored in the database.";
} else {
echo "Error storing security questions and answers in the database.";
}
?>