开发者

Looping correctly though array

Okay so I'm looping through the results that contains two question IDs and two answers and I'm trying to match the two answers with the two answers from the form submission. I'm not sure what I'm doing wrong.

<?php

// Include the database page
require ('../inc/dbconfig.php');
require ('../inc/global_functions.php');

//Login submitted
if (isset($_POST['submit'])) {

// Errors defined as not being any
$errors = false;

if (trim($_POST['answer1']) == '') { $errors = true; }
if (trim($_POST['answer2']) == '') { $errors = true; }

// Error checking, make sure all form fields have input
if ($errors) {

    // Not all fields were entered error
    $message = "You must enter values to all of the form fields!";

    $output = array('errorsExist' => $errors, 'message' => $message);

} else {

    $userID = mysqli_real_escape_string($dbc,$_POST['userID开发者_运维知识库']);
    $answer1Post = mysqli_real_escape_string($dbc,$_POST['answer1']);
    $answer2Post = mysqli_real_escape_string($dbc,$_POST['answer2']);
    $question1 = mysqli_real_escape_string($dbc,$_POST['question1']);
    $question2 = mysqli_real_escape_string($dbc,$_POST['question2']);

    $query = "SELECT * FROM manager_users_secretAnswers WHERE userID = '".$userID."'";
    $result = mysqli_query($dbc,$query);

    // Count number of returned results from query
    if (mysqli_num_rows($result) > 0) {

        while ($row = mysqli_fetch_array($result)) {   

            $answer = $row['answer']; 

            // Comparing the database password with the posted password
            if (($answer == $answer1Post) && ($answer == $answer2Post)) {



            } else {

                $errors = true;
                $message = "Your answers did not match the answers inside the database!";

                $output = array('errorsExist' => $errors, 'message' => $message);

            }


        }

    } else {

        $errors = true;
        $message = "We did not find any answers for your questions! Please consult the site administrator!";

        $output = array('errorsExist' => $true, 'message' => $message);

    }

}

}

//Output the result
$output = json_encode($output);
echo $output;

?>


Since your question is not clear in the first place, so I'm assuming that the question you are asking is "why you're not getting any matching results, when you've the correct answers in the database?". Please correct me, if this is wrong.

The logic can be like this:-

<?php

// Include the database page
require ('../inc/dbconfig.php');
require ('../inc/global_functions.php');

// Login submitted
if (isset($_POST['submit'])) {

    // Errors defined as not being any
    $errors = false;

    if (trim($_POST['answer1']) == '') { $errors = true; }
    if (trim($_POST['answer2']) == '') { $errors = true; }

    // Error checking, make sure all form fields have input
    if ($errors) {

        // Not all fields were entered error
        $message = "You must enter values to all of the form fields!";

        $output = array('errorsExist' => $errors, 'message' => $message);

    } else {

        $userID = mysqli_real_escape_string($dbc, $_POST['userID']);
        $answer1Post = mysqli_real_escape_string($dbc, $_POST['answer1']);
        $answer2Post = mysqli_real_escape_string($dbc, $_POST['answer2']);
        $question1 = mysqli_real_escape_string($dbc, $_POST['question1']);
        $question2 = mysqli_real_escape_string($dbc, $_POST['question2']);

        $query = "SELECT * FROM manager_users_secretAnswers WHERE userID = '".$userID."'";
        $result = mysqli_query($dbc, $query);

        // Count number of returned results from query
        if (mysqli_num_rows($result) > 0) {

            while ($row = mysqli_fetch_array($result)) {

                $answer = $row['answer'];

                // Comparing the database password with the posted password
                if ($answer == $answer1Post) {

                    // The first answer is correct
                    $errors = false;
                    $message = "Your first answer is correct!";

                } else if ($answer == $answer2Post) {

                    // The second answer is correct
                    $errors = false;
                    $message = "Your second answer is correct!";

                } else {

                    $errors = true;
                    $message = "Your answers did not match the answers inside the 

                }

                $output = array('errorsExist' => $errors, 'message' => $message);

            }

        } else {

            $errors = true;
            $message = "We did not find any answers for your questions! Please consult the site administrator!";

            $output = array('errorsExist' => $true, 'message' => $message);

        }

    }

}

// Output the result
$output = json_encode($output);
echo $output;

?>

It's better to have more segregation of logical conditions. In this case, it's your two answers to check for.

Hope it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜