我有一個網頁(我是初學php,僅在1.5個月以下的書籍示例中),它允許用戶輸入一個gpa,然後搜索符合輸入最小值的學生。當點擊搜索按鈕時,它會從php文件中調用一個函數來查詢數據庫。我的代碼幾乎是正確的。問題是它返回所有學生,我只想返回滿足輸入最小gpa的行。試圖使用HAVING條款和其他人,但仍然不會返回我想要的。謝謝!如何僅返回符合用戶搜索條件的選定行
連接到SQL小提琴:http://www.sqlfiddle.com/#!2/be9da
HTML:
<script type="text/javascript">
function queryStudent() {
var ajaxRequest = new XMLHttpRequest;
ajaxRequest.onreadystatechange = function() {
if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
document.getElementById("ajax_output").innerHTML = ajaxRequest.responseText;
}
};
ajaxRequest.open("GET", "gpa_search.php", true);
ajaxRequest.send(null);
}
</script>
<form name="student">
<label>Minimum GPA: </label>
<input type="text" id="GPA" name="gpa"><br><br>
<input type="button" onclick="queryStudent();" value="Search" id="button">
</form><br>
<!--output section after search-->
<section id="ajax_output">
</section><br><br>
<p>Students with higher than minimum GPA will be displayed here.</p><br>
<a href="search_split.htm">Search & Split</a>
PHP:
<?php
//get user input from text box on index.htm
$GPA = filter_input(INPUT_GET, 'GPA');
//Code for query
$query = 'SELECT *
FROM student
ORDER BY studentID';
$statement = $db->prepare($query);
$statement->bindValue(':GPA', "%".$GPA."%", PDO::PARAM_STR);
$statement->execute();
$students = $statement->fetchAll();
?>
<!--table to hold output-->
<table>
<tr>
<th>Student ID</th>
<th>Name</th>
<th>Email</th>
<th>GPA</th>
</tr>
<?php foreach ($students as $student) : ?>
<tr>
<td><?php echo $student['studentID']; ?></td>
<td><?php echo $student['name']; ?></td>
<td><?php echo $student['email']; ?></td>
<td><?php echo $student['GPA']; ?></td>
</tr>
<?php endforeach; ?>
</table>
<body>
</body>
</html>
沒有發佈任何方法。儘管它必須是sql查詢,但某些工作並不正確。 – allendks45