2017-04-18 93 views
1

我爲用戶輸入了一個表格,用於輸入員工的名字和姓氏。提交用戶時,必須從僱員數據庫中選擇條目與employees表中的first_name列相同的所有內容。當我這樣做時,它不會顯示在頁面上,所以我相信肯定會出現一些我找不到的錯誤。根據用戶輸入顯示來自數據庫的結果

 <!doctype html> 
<html lang="en"> 
<head> 
    <link href="employeeStyles.css" rel="stylesheet"> 
    <title>Employee Search</title> 
</head> 
<body> 
    <div id="employeeArea"> 
     <h1>Employee Search Results</h1> 
     <?php 
     $firstName = $_GET['firstName']; 
     $lastName = $_GET['lastName']; 
     $resultsNumber = $_GET['resultsNumber']; 


     @ $db = new mysqli('localhost','root','','employees'); 

     $firstName = $db->real_escape_string($firstName); 
     $lastName = $db->real_escape_string($lastName); 
     $resultsNumber = $db->real_escape_string($resultsNumber); 

     if (mysqli_connect_errno()){ 
      echo 'Error: Could not connect to the database. Please try again later. </body></html>'; 
      exit; 
     } 
     $query = "SELECT * FROM employees WHERE first_name LIKE .$firstName.'%'"; 
     $result = $db->query($query); 
     $numResults = $result->num_rows; 

     echo 'Number of results found '.$numResults; 

     for ($i=0; $i<$result; $i++){ 
      $row = $result->fetch_assoc(); 
      echo $row ['first_name']."<br>"; 
      echo $row ['last_name']."<br>"; 
      echo $row ['emp_no']."<br>"; 
      echo $row ['hire_date']."<br>"; 
      echo $row ['birth_date']."<br>"; 
      echo $row ['gender']."<br>"; 
     } 

     $db->close(); 
     ?> 
    </div> 
</body> 

+1

'LIKE $的firstName '%''是一個語法錯誤和'mysqli_error($ DB)'會告訴你這件事。 –

回答

1

好了,問題在查詢中規定。

您傳遞給查詢的變量未包含在引號內。 嘗試:。

$query = "SELECT * FROM employees WHERE first_name LIKE '" . $firstName . "%'"; 
+0

該查詢的工作原理,但我現在得到一個錯誤類mysqli_result的對象無法轉換爲int,我認爲這是我的循環顯示結果 –

+0

嘗試'for($ i = 0; $ i < $ numResults; $ i ++){'爲你的循環。 – Thoby