2011-06-24 39 views
0

好了,所以我通過循環包含兩個問題ID和兩個答案,結果和我想的兩個答案從表單提交的兩個答案匹配。 我不知道我在做什麼錯。循環雖然正確陣列

<?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; 

?> 
+1

你沒有得到預期的輸出?你有什麼錯誤嗎? – 2011-06-24 03:44:47

回答

1

因爲你的問題不是擺在首位清楚,所以我假設你問的問題是「爲什麼你沒有得到任何匹配的結果,當你已經在正確的答案數據庫?「。如果這是錯誤的,請糾正我。

的邏輯可能是這樣的: -

<?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; 

?> 

最好是有邏輯條件更加隔離。在這種情況下,這是您檢查的兩個答案。

希望它有幫助。