How can PHP be used to retrieve and display user-selected security questions during password recovery processes?

To retrieve and display user-selected security questions during a password recovery process, you can store the security questions in a database table along with the user's information. When a user requests a password reset, you can retrieve their security questions from the database and display them on the password recovery form for verification.

// Retrieve user's security questions from the database
$user_id = $_SESSION['user_id']; // Assuming user is logged in
$query = "SELECT question1, question2, question3 FROM security_questions WHERE user_id = $user_id";
$result = mysqli_query($conn, $query);

if(mysqli_num_rows($result) > 0) {
    $questions = mysqli_fetch_assoc($result);
    // Display security questions on the password recovery form
    echo "<label>{$questions['question1']}</label><br>";
    echo "<input type='text' name='answer1'><br>";
    echo "<label>{$questions['question2']}</label><br>";
    echo "<input type='text' name='answer2'><br>";
    echo "<label>{$questions['question3']}</label><br>";
    echo "<input type='text' name='answer3'><br>";
}