简体   繁体   中英

ORDER BY Year DESC Not working

I have some info im displaying on my page. I got it to display the content of my table perfectly. However when i try to sort it everything breaks. Any advice will be appreciated

$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). "

"

;

Here is my complete code.

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

?>

Your ORDER BY comes after WHERE condition.

Also use empty() or isset() with post values to prevent from undefine index error.

$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 
"
;
$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 
"

;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM