我的页面上显示了一些信息。我得到它完美地显示我的表的内容。但是,当我尝试排序时,一切都会中断。任何建议将不胜感激
$sql = "
SELECT *
FROM
agreements ORDER BY Year DESC
WHERE
1 = 1
".($_POST['agreeno'] != '' ? "AND Resolution = '".$_POST['agreeno']."'" : Null)."
".($_POST['keyword'] != '' ? "AND Resolution_Name LIKE '%".$_POST['keyword']."%'" : Null)."
".($_POST['Year'] != '' ? "AND Year LIKE '%".$_POST['Year']."%'" : Null). "
"
;
这是我的完整代码。
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "<table class='table table-striped publications' border='0' cellspacing='0' cellpadding='0'>
<tbody>
<tr>
<td style='text-align: left;' align='center'><strong>Resolution Name</strong></td>
<td ><strong>Resolution</strong></td>
<td><strong>Year</strong></td>
<td><strong>Department</strong></td>
<td><strong>Document</strong></td>
</tr>";
$sql = "
SELECT *
FROM
agreements ORDER BY Year DESC
WHERE
1 = 1
".($_POST['agreeno'] != '' ? "AND Resolution = '".$_POST['agreeno']."'" : Null)."
".($_POST['keyword'] != '' ? "AND Resolution_Name LIKE '%".$_POST['keyword']."%'" : Null)."
".($_POST['Year'] != '' ? "AND Year LIKE '%".$_POST['Year']."%'" : Null). "
"
;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr><td style='text-align: left' align='center'>" . $row["Resolution_Name"]. "</td><td style='text-align: left;' align='center'> " . $row["Resolution"]. " </td><td style='text-align: left;' align='center'> " . $row["Year"]. " </td><td style='text-align: left;' align='center'> " . $row["Department"]. "</td><td style='text-align: left;' align='center'> <a href='". $row["Document"]. "' target='_empty'>View Document [PDF]</a> </td></tr>";
}
} else {
echo "0 results";
}
$conn->close();
echo "</tr></table>";
?>
答案 0 :(得分:1)
ORDER BY
来自WHERE
条件。
还可以将empty()
或isset()
与帖子值一起使用,以防止出现未定义的索引错误。
$sql = "
SELECT *
FROM
agreements
WHERE
1 = 1
".((!empty($_POST['agreeno'])) ? "AND Resolution = '".$_POST['agreeno']."'" : Null)."
".((!empty$_POST['keyword'])) != '' ? "AND Resolution_Name LIKE '%".$_POST['keyword']."%'" : Null)."
".((!empty($_POST['Year'])) != '' ? "AND Year LIKE '%".$_POST['Year']."%'" : Null). "
ORDER BY Year DESC
"
;
答案 1 :(得分:0)
$sql = "
SELECT *
FROM
agreements
WHERE
1 = 1
".($_POST['agreeno'] != '' ? "AND Resolution = '".$_POST['agreeno']."'" : "")."
".($_POST['keyword'] != '' ? "AND Resolution_Name LIKE '%".$_POST['keyword']."%'" : "")."
".($_POST['Year'] != '' ? "AND Year LIKE '%".$_POST['Year']."%'" : ""). "
ORDER BY Year DESC
"
;