通过下拉菜单筛选表格.您的SQL语法出现错误;


Filtering table via dropdown menu. You have an error in your SQL syntax;

我正在尝试创建一个房地产上市网站,并让用户能够按代理和郊区进行筛选。目前我收到以下消息:"您的SQL语法有错误;请查看与MySQL服务器版本对应的手册,在第1行'$sql1'附近找到要使用的正确语法"目前,我只专注于让第一个过滤器工作,即代理下拉列表中的"J Blades",这就是为什么其他过滤器不完整或完全错误的原因。

<!DOCTYPE html>

<meta charset="UTF-8" />
<title>Pharcourts</title>
<link href="global.css" rel="stylesheet" type="text/css"/>

<div id="container">
<div id="container-background">

    <div id="header">

    <div id="nav">
            <ul>
                <li class="home"><a href="index.php">Home</a></li>
                <li class="listings"><a href="staffornot.php">Listings</a></li>
                <li class="agents"><a href="agents.php">Agents</a></li>
                <li class="contact"><a href="contact.php">About/Contact</a></li>
                <li class="login"><a href="login.php">Staff Login</a></li>
            </ul>
    </div>

    </div>

    <div id="sidebar">
        <h2>On This Page:</h2>
        <ul>
            <li><a href="#listings">Listings</a></li>
        </ul>

    </div>

    <div id="content">

        <a id="listings"></a>
        <h1>Current Listings:</h1>
        <h3><a href='logout.php'>Logout<a></h3>
    <?php
        session_start();
        if(!isset($_SESSION['username'])){
        header("Location:Login.php");
        }
        $con=mysqli_connect("localhost", "root", "") or die(mysqli_error());
        mysqli_select_db($con,"alexdb") or die(mysqli_error());
        Print "<form action='listingsstaff.php' method='post'>";
        $sql1 = "SELECT * FROM tblhousingstock WHERE agent='J Blades'";
        Print "Agent";
        Print "<select name='filteragent'>";
        Print "<option value='SELECT * FROM tblhousingstock'>All</option>";
        Print '<option value="$sql1")>J Blades</option>';
        Print "<option value='SELECT * FROM tblhousingstock WHERE agent=D Astle'>D Astle</option>";
        Print "<option value='SELECT * FROM tblhousingstock WHERE agent=M Smith'>M Smith</option>";
        Print "</select>";

        Print "Suburb";
        $data1=mysqli_query($con,"SELECT tblhousingstock.add2 FROM tblhousingstock GROUP BY tblhousingstock.add2") or die(mysqli_error());

        Print "<select name='filtersuburb' style=width:80px>";
        Print "<option value='all'>All</option>";
        while($info1 = mysqli_fetch_array ($data1))
        {
        Print "<option value=".$info1['add2'].">".$info1['add2']."</option>";
        }
        Print "</select>";
        Print "<input type='submit'>";
        Print "</form>";


        $data2=mysqli_query($con, $_POST['filteragent']) or die(mysqli_error($con));

        Print "<table border cellpadding=3>";
        Print "<th>Address:</th><th>Suburb:</th><th>City:</th><th>Asking Price:</th><th>Number of Bathrooms:</th><th>Number of Bedrooms:</th><th>Agent:</th>";
        while($info2 = mysqli_fetch_array ($data2))
        {
        Print "<tr>";
        Print "<td>".$info2['add1']."</td>";
        Print "<td>".$info2['add2']."</td>";
        Print "<td>".$info2['add3']."</td>";
        Print "<td>$".$info2['price']."</td>";
        Print "<td>".$info2['bath']."</td>";
        Print "<td>".$info2['bed']."</td>";
        Print "<td>".$info2['agent']."</td>";
        }
        Print "</table>";
    ?></p>

    </div>

    <div id="footer">
    </div>

</div>
</div>

Print '<option value="$sql1")>J Blades</option>';

您实际上并没有在这里进行查询,您只是将文本分配给了一个变量。

编辑:

你还有很长的路要走才能使这项工作正常进行,但希望这将修复你认为错误的地方:

    ..........
Print "<form action='listingsstaff.php' method='post'>";
    Print "Agent";
    Print "<select name='filteragent'>";
    Print "<option value='"SELECT * FROM tblhousingstock'">All</option>";
    Print "<option value='"SELECT * FROM tblhousingstock WHERE agent='J Blades''">J Blades</option>";
    Print "<option value='"SELECT * FROM tblhousingstock WHERE agent='D Astle''">D Astle</option>";
    Print "<option value='"SELECT * FROM tblhousingstock WHERE agent='M Smith''">M Smith</option>";
    Print "</select>";
    Print "Suburb";
    ..........

我只是改变了用引号和双引号括起select语句的方式。