从php中只选择一个下拉框

时间:2015-06-29 15:17:10

标签: php

我有两个列表框,我点击它们两个来创建SELECT查询。我将$ POST变量放入另一个变量,然后将它们放入选择查询。这似乎工作正常,但问题出现时我只想从其中一个盒子中选择,例如肯·戴维斯的所有书籍或冒险类型的所有书籍。在我得到结果之前,似乎我必须选择两个盒子。任何人都可以建议一个方向此

<html>
<head>
<title>My Page</title>
</head>
<body>
<br>
<form name="myform" action="dropdown2.php" method="POST">

<select name="author" size="4">
<option value="ken davies">ken davies</option>
<option value= "arthur smith">arthur smith</option>
<option value="gill rafferty">gill rafferty</option><br />
<option value="molly brown">molly brown</option><br />
<option value="gilbert riley">gilbert riley</option><br />
<input type = "submit" name = "submit" value = "go">

<select name="genre" size="4"> 
<option value="adventure">adventure</option>
<option value="biography">biography</option>
<option value="crime">crime</option><br />
<option value="romance">romance</option>
<option value="2007">thriller</option>

<input type = "submit" name = "submit" value = "go">


<?php

$_POST['author'];
$bird = $_POST['author'];
$_POST['genre'];
$cat = $_POST['genre'];


$con = mysql_connect("localhost","root","");
If (!$con){
    die("Can not Connect with database" .  mysql_error());
}
 Mysql_select_db("authors",$con);
$sql = "SELECT * FROM books WHERE author = '$bird' AND genre = '$cat' ";
$myData = mysql_query($sql,$con);

echo"<table border=1>
<tr>id</th>
<tr>author</th>
<tr>title</th>
<tr>publisher</th>
<tr>year</th>
<tr>genre</th>
<tr>sold</th>
</tr>";


while($record = mysql_fetch_array($myData)){
    echo "<tr>";
    echo "<td>" . $record['id'] . "</td>";
    echo "<td>" . $record['author'] . "</td>";
    echo "<td>" . $record['title'] . "</td>";
    echo "<td>" . $record['publisher'] . "</td>";
    echo "<td>" . $record['year'] . "</td>";
    echo "<td>" . $record['genre'] . "</td>";
    echo "<td>" . $record['sold'] . "</td>";

    echo "<tr />";
}
echo "</table>";



mysql_close($con);



?>



</form>
</body>
</html>   

3 个答案:

答案 0 :(得分:0)

如果您只想按作者或类型进行过滤,请更改导致SQL的逻辑。

if (isset($bird) && isset($cat))
    $sql = "SELECT * FROM books WHERE author = '$bird' AND genre = '$cat' ";
elseif (isset($bird))
    $sql = "SELECT * FROM books WHERE author = '$bird' ";
elseif (isset($cat))
    $sql = "SELECT * FROM books WHERE genre = '$cat' ";

此外,法律要求我知道在SQL中放置变量会导致SQL注入。你应该准备和执行。我将在下面写一些示例代码(一秒)。

答案 1 :(得分:0)

首先,您需要从表单中获取值,但是您需要添加一些允许它们是非强制性的代码:

$bird = ( ! empty($_POST['author'])) ? $_POST['author'] : null;
$cat  = ( ! empty($_POST['genre']))  ? $_POST['genre']  : null;

这将为您提供随表单提交的值,或null以后可用于决定如何查询数据库的值。我还建议您检查给定的值与您的预期相符。因此,要么将数据与已知作者或已知类型进行比较,要么清除篡改数据,例如:

$genres = array('adventure', 'biography', 'crime', 'romance', 'thriller');
if ( ! in_array($cat, $genres)) {
    // invalid data supplied (you could show an error)
    unset($cat); // destroys the invalid variable
}

然后,当您进入数据库查询时,您可以执行以下操作:

if (isset($bird) && isset($cat)) {
    // SELECT by both
}
else if (isset($bird)) {
    // SELECT by author
}
else if (isset($cat)) {
    // SELECT by cat
}
else {
    // SELECT all
}

有很多方法可以缩短上述内容,但这对你来说是一个很好的起点。

答案 2 :(得分:0)

在这种情况下,您必须在PHP代码中为不同的情况编写单独的查询。

//When both the list boxes are selected
if(isset($_POST['author'])&&isset($_POST['genre']))
{
    $sql = "SELECT * FROM books WHERE author = '$bird' AND genre = '$cat' ";
    unset($_POST['genre']);
    unset($_POST['author']);
}
//When searching genre-wise (author variable not set)
elseif(!isset($_POST['author']))
{
    $sql = "SELECT * FROM books WHERE genre = '$cat' ";
    unset($_POST['genre']);
}
//When searching authorwise (genre variable not set)
elseif(!isset($_POST['genre']))
{
    $sql = "SELECT * FROM books WHERE author = '$bird'";
    unset($_POST['author']);
}