SQL查询无法通过PHP正常工作

时间:2018-03-28 13:15:05

标签: php html sql

请不要讨厌,因为我在这里找到了解决问题的方法。我正在使用xampp来运行这个PHP代码。问题是搜索功能完美地运行但由于某种原因我的if语句改变了它的排序顺序。所有帮助表示赞赏。所有人都不爱恨。感谢。

    <html>
<head>  

<title>SeedBase</title>
<link rel="stylesheet" type="text/css" href="CSSstyle.css">

</head>

<body>
<h1 class="logo">SeedBase</h1>

<ul div class="navbar">
    <li><a href="HTMLhome.HTML">Home     |</a></li>
    <li><a href="PHPseeds.php">Seeds     |</a></li>
    <li><a href="HTMLcontact.html">Contact</a></li>
</ul>


<div class="search">
<form method="POST" action="PHPseeds.php">
    <p>Search SeedBase:<input type="text" name="searchbox" value="seed"></p>
        <p><select name="selector">
        <optgroup label = "Sort By">
            <option value="def" selected>Defult</option>
            <option value="asc" >Quantity Asc</option>
            <option value="desc">Quantity Desc</option>
        </optgroup>
    </select></p>

        <p><input type="submit" name="button" value="GO"></p>
</form>
</div>





<?php
echo "<phpcss>";
include 'db.inc.php';

$row = 0;
$find = $_POST['searchbox'];
$choice = $_POST['selector'];
$output = "";

    if ($find != '') {
        if ($choice="def"){
        $sql =  "SELECT * FROM seeds WHERE  name LIKE '%$find%' OR description LIKE  '%$find%' "; 
        }
        else if ($choice="asc"){
        $sql =  "SELECT * FROM seeds WHERE  name LIKE '%$find%' OR description LIKE  '%$find%' ORDER BY quantity ASC "; 
        }
        else if ($choice="desc"){
        $sql =  "SELECT * FROM seeds WHERE  name LIKE '%$find%' OR description LIKE  '%$find%' ORDER BY quantity DESC "; 
        }
    }

    if ($find == ''){
        if ($choice="Defult"){
        $sql =  "SELECT * FROM seeds "; 
        }
        else if ($choice="asc"){
        $sql =  "SELECT * FROM seeds WHERE  name LIKE '%$find%' OR description LIKE  '%$find%' ORDER BY quantity ASC "; 
        }
        else if ($choice="desc"){
        $sql =  "SELECT * FROM seeds WHERE  name LIKE '%$find%' OR description LIKE  '%$find%' ORDER BY quantity DESC "; 
        }

    }
$result = $pdo->query($sql);
?>



<div class="DBpic">
<?php
    echo "<table>";
foreach ($result as $row)
{
    $seed = $row['name'];
    $description = $row['description'];
    $quantity = $row['quantity'];
    $image = $row['image'];
    $output ="<tr><tr> $seed <tr> $quantity cases" ;

    echo "<table>";
    echo  '<br><img src="data:image/jpeg;base64,' . base64_encode( $row['image'] ) . '" /><br>'. $output;
    echo "</table>";

    }
    echo "</table>";



?>
</div>




<?php
if ($row == 0)
{
echo "nothing could be found for that search term";
}
echo "</phpcss>";
?>


</body>
</html>

1 个答案:

答案 0 :(得分:0)

在第二个If条件中,您必须使用!=

 if ($find != '')