2011-06-23 72 views
1

我想弄清楚的是如何訪問我需要的不同數組值。我有以下查詢,並在應用print_r()時返回數組。出於某種原因,它只會從db表中執行第一行。它應該返回一整行。沒有返回足夠的行

<?php 

session_start(); 

// 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 = "no"; 

if((empty($_POST['answer1'])) || (trim($_POST['answer1'])=="") || ($_POST['answer1'] == NULL) || (!isset($_POST['answer1']))){$errors = "yes";} 
if((empty($_POST['answer2'])) || (trim($_POST['answer2'])=="") || ($_POST['answer2'] == NULL) || (!isset($_POST['answer2']))){$errors = "yes";} 

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

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

    $output = array('errorsExist' => true, '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 == $answerPost) { 



      } else { 

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

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

      } 


     } 

    } else { 

     $errors = "yes"; 
     $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; 

?> 

回答

4

因爲你只取第一個,你應該對結果集循環改爲:

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

if (mysqli_num_rows($result) > 0) { 
    while ($row = mysqli_fetch_array($result)) { 
     print_r($row); 
    } 
} 

順便說一句,你應該使用prepared statements避免SQL injection

+0

謝謝。我更新了我的代碼。我唯一的問題是,它只是檢查最後一行,或者它將檢查兩行。 –

+1

它將循環訪問結果集中的* all *行。如果你有兩行,那麼它將返回兩個。 – Benjamin

+0

嗯,我很好奇,知道如何能夠測試答案1在數據庫中的用戶的第一個答案的Post1和在數據庫中的用戶的第二個答案的answer2Post。 –

2

你需要在循環中包裝你的提取。例如

if (mysqli_num_rows($result) > 0) 
{ 
    while (($row = mysqli_fetch_array($result)) !== false) 
    { 
    if ($row['answer'] == $answerPost) 
    { 
     // $row matches what we're looking for 
    } 
    else 
    { 
     $errors = "yes"; 
     $message = "Your answers did not match the answers inside the database!"; 
     $output = array('errorsExist' => true, 'message' => $message); 
    } 
    print_r($row); 
    } 
} 
+0

謝謝。我更新了我的代碼。我唯一的問題是,它只是檢查最後一行,或者它將檢查兩行。 –

+1

@Jeff:請看我更新的答案。 –

+0

但是,這將如何處理來自用戶輸入的兩個不同的帖子答案。 –

相關問題