2013-04-27 147 views
-3

我能夠連接到數據庫,但是,我無法查看搜索查詢的結果。每當我在「學生證」中輸入一個值時,該字段將被刪除,並且不會顯示任何結果。任何幫助將不勝感激,我可能做錯了什麼。另外,是的,我的SQL表具有所有這些列。php搜索查詢結果未顯示

search1.php

 <html> 
    <head> 
    <title>Student Search</title> 
    <style type="text/css"> 
    table { 
    background-color: #FCF; 
    } 

    th { 
    width: 150px; 
    text-align:left; 
    } 
    </style> 
    </head> 
    <body> 
    <h1>Student Search</h1> 
    <form method = "post" action="search1.php"> 
    <input type="hidden" name="submitted" value="true" /> 
    <label>Student ID: 
    <select name="category"> 
    <option value="ID">ID</option> 
    </select> 
    </label> 
    <label>Search Criteria: <input type="text" name="criteria"/></label> 
    </form> 
    <?php 

    if (isset($_POST['submitted'])) { 
    //connect to the database 
    include ('mcon.php'); 

    $category = $_POST['category']; 
    $criteria= $_POST['criteria']; 
    $query = "SELECT * FROM student_info WHERE $category = '$critera'"; 
    $result = mysqli_query($dbcon, $query) or die('Error getting data.'); 

    echo "<table>"; 
    echo "<tr> 
    <th>ID</th> 
    <th>Project</th> 
    <th>Starter Project</th> 
    <th>Course</th> 
    <th>KDs Completed in your Course</th> 
    <th>Projects Completed</th> 
    <th>Pro 

ject 1</th> 
<th>P1KD1</th> 
<th>P1KD2</th> 
<th>P1KD3</th> 
<th>P1KD4</th> 
<th>P1KD5</th> 
<th>Project 2</th> 
<th>P2KD1</th> 
<th>P2KD2</th> 
<th>P2KD3</th> 
<th>P2KD4</th> 
<th>P2KD5</th> 
<th>Project 3</th> 
<th>P3KD1</th> 
<th>P3KD2</th> 
<th>P3KD3</th> 
<th>P3KD4</th> 
<th>P3KD5</th> 
<th>Project 4</th> 
<th>P4KD1</th> 
<th>P4KD2</th> 
<th>P4KD3</th> 
<th>P4KD4</th> 
<th>P4KD5</th> 
</tr>"; 
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) { 
echo "<tr><td>"; 
echo $row['ID']; 
echo "</td><td>"; 
echo $row['Project']; 
echo "</td><td>"; 
echo $row['Starter Project']; 
echo "</td><td>"; 
echo $row['Course']; 
echo "</td><td>"; 
echo $row['KDs completed in your course']; 
echo "</td><td>"; 
echo $row['Projects Completed']; 
echo "</td><td>"; 
echo $row['Project 1']; 
echo "</td><td>"; 
echo $row['P 1 K D 1']; 
echo "</td><td>"; 
echo $row['P 1 K D 2']; 
echo "</td><td>"; 
echo $row['P 1 K D 3']; 
echo "</td><td>"; 
echo $row['P 1 K D 4']; 
echo "</td><td>"; 
echo $row['P 1 K D 5']; 
echo "</td><td>"; 
echo $row['Project 2']; 
echo "</td><td>"; 
echo $row['P 2 K D 1']; 
echo "</td><td>"; 
echo $row['P 2 K D 2']; 
echo "</td><td>"; 
echo $row['P 2 K D 3']; 
echo "</td><td>"; 
echo $row['P 2 K D 4']; 
echo "</td><td>"; 
echo $row['P 2 K D 5']; 
echo "</td><td>"; 
echo $row['Project 3']; 
echo "</td><td>"; 
echo $row['P 3 K D 1']; 
echo "</td><td>"; 
echo $row['P 3 K D 2']; 
echo "</td><td>"; 
echo $row['P 3 K D 3']; 
echo "</td><td>"; 
echo $row['P 3 K D 4']; 
echo "</td><td>"; 
echo $row['P 3 K D 5']; 
echo "</td><td>"; 
echo $row['Project 4']; 
echo "</td><td>"; 
echo $row['P 4 K D 1']; 
echo "</td><td>"; 
echo $row['P 4 K D 2']; 
echo "</td><td>"; 
echo $row['P 4 K D 3']; 
echo "</td><td>"; 
echo $row['P 4 K D 4']; 
echo "</td><td>"; 
echo $row['P 4 K D 5']; 
echo "</td></tr>"; 
} 

echo "</table>"; 

} //end of main if statement 

?> 

</body> 
</html> 

感謝任何幫助! 〜Carpetfizz

+0

投票關閉的問題缺少很多細節,包括(但不限於)表結構,並且過於具體。 – 2013-04-27 17:28:42

+0

@SébastienRenauld表格以頂部各自名稱的列組織。行以「ID」開頭。我基本上想要檢索ID,然後顯示以該ID開頭的行。 – Carpetfizz 2013-04-27 17:40:08

回答

2

您的查詢一個錯字,你有$critera,你的意思$criteria看到:

$query = "SELECT * FROM student_info WHERE $category = '$critera'"; 
            YOU LACK AN i HERE -------^ 
+0

或在這裏添加額外的'i' $ criteria = $ _POST ['criteria'];'。 – hjpotter92 2013-04-27 16:52:27

+0

謝謝你指出。但是,問題仍然存在。 – Carpetfizz 2013-04-27 17:05:35