并非所有下拉框都在工作

时间:2015-07-08 15:11:59

标签: php html

我有四个下拉框..作者,年份,流派和出版商。如果我点击所有4个框它工作正常,所以我可以看到作者在任何一年写的书,这伴随着流派和出版商。 如果我只是点击一个框,我也会得到一个结果。例如,如果我点击类型框中的“冒险”,我会获得冒险的所有记录。与其他框分别相同。 所以这些盒子正在一起工作并且是个性化的。当我尝试点击两个(或三个)盒子时即出现问题,即如果我只想要作者和出版商,或者流派,年份和出版商。所有建议都非常感谢:)

<html>
<head>
<title>My Page</title>
</head>
<body>
<br>
<form name="myform" action="dropdown3.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">
<select name="year" size="4">
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>

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

<select name="publisher" size="4">
<option value="blue parrot">blue parrot</option>
<option value="yonkers">yonkers</option>
<option value="zoot">zoot</option>

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



<?php


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

//NEXT CONNECT TO DATABASE
//------------------------------------------------------------------
//echo $bird;
$con = mysql_connect("localhost","root","");
If (!$con){
    die("Can not Connect with database" .  mysql_error());
}
 Mysql_select_db("authors",$con);



if (isset($bird) && isset($cat) && isset($mouse) && isset($goat))
{  
    $sql = "SELECT * FROM books WHERE author = '$bird' AND genre = '$cat' AND year= '$mouse' AND publisher = '$goat' ";
}
else if (isset($bird)) 
{ 
    $sql = "SELECT * FROM books WHERE author = '$bird' ";
}
else if (isset($cat))
{
    $sql = "SELECT * FROM books WHERE genre = '$cat' ";

}
else if (isset($mouse)) 
{   
        $sql = "SELECT * FROM books WHERE year = '$mouse' ";    
}   


else if (isset($goat)) 
{
    $sql = "SELECT * FROM books WHERE publisher = '$goat' ";    


$myData = mysql_query($sql,$con);

echo"<table border=1>

<tr>
<th>id</th>
<th>author</th>
<th>title</th>
<th>publisher</th>
<th>year</th>
<th>genre</th>
<th>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);



?>
 all four are working<br />
    all work individually<br />
    two or three dont work together

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

2 个答案:

答案 0 :(得分:1)

你应该做一些不太复杂的事情,只有一个查询就足够了,例如

$sql = "SELECT * FROM books WHERE 1 = 1";

然后根据需要添加条件

if(!is_null($bird)) {
    $sql .= " AND author = '$bird'";
}
if(!is_null($cat)) {
    $sql .= " AND genre = '$cat'";
}
if(!is_null($mouse)) {
    $sql .= " AND year = '$mouse'";
}
if(!is_null($goat)) {
    $sql .= " AND publisher = '$goat'";
}

注意:如果你不想迷路,你应该为你的变量写真实姓名。

答案 1 :(得分:0)

尝试这样的事情:

$sql =  "SELECT * FROM books ";
$where  = "";
if (isset($bird)) {
    if ($where == "") {
        $where = "WHERE author = " . $bird . " ";

    }  else {
        $where = " AND author = " + $bird ." ";
    }
}

if (isset($cat)) {
    if ($where == "") {
        $where = "WHERE genre = " . $cat . " ";

    }  else {
        $where = " AND genre = " + $cat ." ";
    }
}

if (isset($mouse)) {
    if ($where == "") {
        $where = "WHERE year = " . $mouse . " ";

    }  else {
        $where = " AND year = " + $mouse ." ";
    }
}

if (isset($goat)) {
    if ($where == "") {
        $where = "WHERE publisher = " . $goat . " ";

    }  else {
        $where = " AND publisher = " + $goat ." ";
    }
}