使用dropbox选择数据库查询

时间:2015-07-22 11:28:48

标签: php

我想在不同的字段上对我的mysql数据库进行排序。 我有3个字段,我想使用表单中的下拉列表更改排序方法。

<html>
<body>
<?php
if(isset($_POST['order']))   {
echo "Du har valgt <b>".$_POST['order']."</b><br><br>";
if ($_POST['order'] = "title")  {
$tur = mysql_query ("SELECT * FROM $tours ORDER BY title ASC"; //mising doubleqoute
}
else
if ($_POST['order'] = "dato")  {
$tur = mysql_query ("SELECT * FROM $tours ORDER BY dato DESC;
}
else 
if ($_POST['order'] = "km")  {
$tur = mysql_query ("SELECT * FROM $tours ORDER BY km DESC;
}
}

 if(mysql_num_rows($tur) > 0) {
          while($a = mysql_fetch_object($tur)) {
           $turid = $a -> id;
           $turd =  $a -> dato;
           ...
 }


?>
<form action="" method="post">
Your sort:
<select name="order" onchange="javascript: submit()">
<option>---</option>
<option value="title">title/option>
<option value ="dato">dato</option>
<option value="km">km</option>
</select>
</form>
</body>
</html>

当我在下拉列表中选择一个值时,我只得到第一个字段的结果:title - 无论我选择了什么。

3 个答案:

答案 0 :(得分:1)

在所有if语句中尝试使用==而不是=

if($ _ POST [&#39; order&#39;] ==&#34; title&#34;)...

希望这有帮助。

问候

答案 1 :(得分:0)

将您的=更改为==

$order_value = "";
if(isset($_POST['order']))   
{
    $order_value  = $_POST['order'];
    echo "Du har valgt <b>".$order_value."</b><br><br>";
    if ($order_value == "title")  {
    $tur = mysql_query ("SELECT * FROM $tours ORDER BY title ASC");
    }
    elseif ($order_value == "dato")  {
    $tur = mysql_query ("SELECT * FROM $tours ORDER BY dato DESC");
    }
    else if ($order_value == "km")  {
    $tur = mysql_query ("SELECT * FROM $tours ORDER BY km DESC");
    }
}

=是赋值运算符

==是比较运算符

这段代码可以解决问题

提交后选择的选项

<select name="order" onchange="javascript: submit()">
<option>---</option>
<option value="title" <?php echo ($order_value!="" && $order_value=="title")? "selected": "" ;?>>title/option>
<option value ="dato" <?php echo ($order_value!="" && $order_value=="dato")? "selected": "" ;?>>dato</option>
<option value="km" <?php echo ($order_value!="" && $order_value=="km")? "selected": "" ;?>>km</option>
</select>

答案 2 :(得分:0)

你需要关闭你的陈述:

$tur = mysql_query ("SELECT * FROM $tours ORDER BY title ASC;

需要:

$tur = mysql_query ("SELECT * FROM $tours ORDER BY title ASC");

最后遗漏了")