我正在開發一個項目,我必須從mysql中顯示數據,我已經成功完成了這項工作,但現在我需要對基於高視圖,低視圖,新添加的結果進行排序舊的,所有這些選項使用選擇下拉菜單。如何通過選擇菜單對mysql結果進行排序
HTML文件
<form action="index.php" method="post" >
<select name="q">
<option value="DESC" name="DESC">High views</option>
<option value="ASC" name="ASC">Low views</option>
</select>
</form>
PHP文件
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "movie_db";
$q = $_Post['q'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT `title`, `poster`, `descrip`, `movie` ,`Duration`, `views`,`director` FROM `movie_db` ORDER BY `views` ".$q ;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<p1>";
// output data of each row
while($row = $result->fetch_assoc())
echo " <div class=\"imgc\"><a href=".$row["movie"]."><img src =" . $row["poster"]. "></a></div></p1><p2><b><a href=" .$row["movie"].">" . $row["title"]. "</a></b></p2><br><p3><b>Duration : </b>".$row["Duration"]." Mins</p3> <p3><b> Views : </b> ".$row["views"]."</p3><br><div><p3><b>Description :</b>".$row["descrip"]."</p3></div><br><p5> By ".$row["director"]."</p5><br>";
} else {
echo "0 results";
}
$conn->close();
?>
</div>
PHP和HTML都在同一個文件,是的index.php
你是如何提交表單? –