php MYSQL查询" ORDER BY"不工作

时间:2016-01-10 03:34:40

标签: php mysql sorting variables

预警:我确定我的代码笨重且效率低下,请随时给我打电话。我对此非常陌生。

我尝试按列排序查询结果,表示为选择输入的结果。选择输入存在于 feed.php 上,其中包含include: 'feedphp2.php

选择输入使用action="feedSort.php"自动提交 onchange feedSort.php 获取提交的值并重定向到feed.php?sort=submittedValue。然后 feed.php 中包含的 feedphp2.php 获取$_GET['sort']值,并提取按该列排序的查询。

// feed.php

<?php
session_start();
if(!isset($_SESSION['userID'])) {
        header('Location: login.html');
}

?>

<html>
<head>
<title>Title</title>

<link href="style.css" type="text/css" rel="stylesheet">
<link href='https://fonts.googleapis.com/css
family=Josefin+Sans:400,100,300,600,700' rel='stylesheet' type='text/css'>
</head>

<body style="background-color:rgba(0,212,242,0.1)">

<div class="headerSmall">
    <div class="container">
        <a href="index.php"><img class="logoImgSmall"
src="images/logosmall2.png"></a>
        <div class="navFeed">
            <?php include 'usernameGet.php';?>,<a 
href="sessionDestroy.php">Log Out</a>
            <a href="addMeal.php">List a Meal</a>
            <a href="dashboardBought.php">Dashboard</a>
        </div>

    </div>
</div>

<div class="container">


    <div class="sort">
    <form action="feedSort.php" method="POST">
        <select name="sortForm" onchange="this.form.submit()">
            <option value="">Sort By...</option>
            <option value="user">By User</option>
            <option value="dateAdded">By Date Added</option>


        </select>
    </form>
    </div>

    <?php include 'feedphp2.php'?>


</div>


</body>
</html>

空间

// feedphp2.php

<?php
include 'MysqlConnect.php';
mysqli_select_db($conn,$dbname);

if (isset($_GET['sort'])) {
    $sortBy = $_GET['sort'];

    echo $sortBy;

    $resultQuery = "SELECT * FROM meals
    ORDER BY '$sortBy'";

} else {
    echo 'sortBy is not set.';
    $resultQuery = "SELECT * FROM meals
    ORDER BY user";
}

$result = mysqli_query($conn, $resultQuery);

//Write to divs
while ($rows = mysqli_fetch_assoc($result)) {

    $imgSrc = $rows['image']; 

    echo '<a href="meal.php?'.'mealID=';
    echo $rows['ID'];
    echo '">';

    echo '<div class="entry">';
    echo '<div class="foodPic">';
    echo '<img src="';

    echo $imgSrc;

    echo '">';
    echo '</div>';
    echo '<p class="entryName">'.$rows['mealName'].'</p>';
    echo '<p class="entryServ">'.$rows['mealQuan'].'</p>';
    echo '<p 
   class="entryPrice">$'.$rows['mealDollars'].'.'.$rows['mealCents'].'</p>';
    echo '<img class="servingsIco" src="images/servings.png">';

    echo '<p class="user">'.$rows['user'].'</p>';
    echo '</div>';
    echo '</a>';
}

?> 

空间 的 // feedSort.php:

<?php
session_start();
$sortBy = $_POST['sortForm'];

header ("Location: feed.php?sort=".$sortBy."");
?>

我的表格中的列同时适用于userdateAdded,并且在查询中明确提供时排序完全排序,即:SELECT * FROM meals ORDER BY user。虽然通过PHP变量提供但是:

$sortBy = user返回与$sortBy = dateAdded完全相同的排序顺序。

感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:1)

使用$ _POST方法传递值并使用$ _GET方法检索它。并更改没有引号的$ sortby。在feedphp2.php中编辑这些行

<?php
include 'MysqlConnect.php';
mysqli_select_db($conn,$dbname);
if (isset($_POST['sort'])) {
    $sortBy = $_POST['sort'];
    echo $sortBy;
    $resultQuery = "SELECT * FROM meals
    ORDER BY $sortBy";

} else {

或使用$ _REQUEST [],方法将检索get和post方法

<?php
include 'MysqlConnect.php';
mysqli_select_db($conn,$dbname);
if (isset($_REQUEST['sort'])) {//request method
    $sortBy = $_REQUEST['sort'];//
    echo $sortBy;
    $resultQuery = "SELECT * FROM meals
    ORDER BY $sortBy";

} else {