如何通过选择菜单对mysql结果进行排序

时间:2015-09-25 08:57:06

标签: php mysql web web-deployment

我正在开发一个项目,我必须显示我已成功完成的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> &nbsp; <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

的同一文件中

4 个答案:

答案 0 :(得分:1)

 $q = $_GET['q'];
        ^

将其与

进行比较
<form action="index.php" method="post" > 
                                 ^

你自己GET你的答案。

答案 1 :(得分:0)

在表单中添加输入类型"submit"

Php和html在index.php 的同一个文件中,在这种情况下将action = ""留空:

<form method="post" action = "">                
    <select name="q">                
        <option value="DESC" name="DESC">High views</option>    
        <option value="ASC" name="ASC">Low views</option>                   
    </select>
    <input type = "submit" value = "Submit">                        
</form>

接下来,改变:

$q = $_GET['q'];

到此:

 $q = $_POST['q'];

由于您在表单中使用了method = "POST",请参阅以下内容:

<form method="post" action = "">

答案 2 :(得分:0)

您是通过POST提交并通过GET获取数据

试试这个

<?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> &nbsp; <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>

答案 3 :(得分:-1)

问题是表单使用POST而你在php中读取GET

要么改变

<form method="post" action = "">

<form method="get" action = "">

或者改变这个

$q = $_POST['q'];

到这个

$q = $_GET['q'];