无法在不同的div中显示来自搜索的Mysql搜索结果

时间:2015-09-30 18:51:39

标签: javascript php html mysql

我试图使用三个分区显示MySQL结果(有和没有搜索)。 Div 1具有用于选择查看结果的单选按钮。 Div 2有一个文本,表明所有成员都显示,一个文本搜索框为Name,一个下拉菜单为Type。搜索结果显示在Div 3中。

当选择所有成员时,文本"显示所有成员"显示在Div 2中,正确的数据显示在Div 3中(正常工作)。选择名称单选按钮后,Div 2中将显示正确的文本搜索框,但我在第3部分中收到以下错误:

警告:mysqli_query():无法在第171行的/home/desgar20/elrenochamber.com/member_dir_test2.php中获取mysqli

警告:mysqli_error()预计在第173行的/home/desgar20/elrenochamber.com/member_dir_test2.php中给出了1个参数0 无法访问数据库:

如果我在搜索框中输入文字并提交,则Div 2和Div 3将变为空白

我正在使用javascript来显示/隐藏搜索的分区和php。

我搜索了答案,但没有找到解决此问题的任何内容。我不知道自己错过了什么。帮助建议。

   

        <style type="text/css">
            .box {
                display: none;
            }
        </style>

        <script type="text/javascript">
            //Show or application part based on selection 
            $(document).ready(function(){
                $('input[type="radio"]').click(function(){
                    if($(this).attr("value")=="all"){
                        $(".box").not(".all").hide();
                        $(".all").show();
                        $(".all_listing").show();
                    }
                    if($(this).attr("value")=="name"){
                        $(".box").not(".name").hide();
                        $(".name").show();
                        $(".all_name").show();
                    }
                    if($(this).attr("value")=="type"){
                        $(".box").not(".type").hide();
                        $(".type").show();
                        $(".all_type").show();
                    }
                });
            });
        </script>
    </head>

    <body>
        <?php
            require_once 'php/dbconnect.php';   // Connect to database
            $connection = db_connect();
        ?>


        <div id="container">

            <div id="service">

                <div id="web">
                    <img width="150px" src="images/search1.png" />
                    <h3>Member <strong><span class="green">Directory</span></strong></h3>
                    <strong>View Members By:</strong><br /><br />
                    <div id="sortOptions">
                        <label><input type="radio" name="sortRadio" value="all"> All Members</label><br />
                        <label><input type="radio" name="sortRadio" value="name"> Name</label><br />
                        <label><input type="radio" name="sortRadio" value="type"> Type</label>
                    </div><!-- sortOptions -->
                </div><!-- end web -->

                <div id="vector">

                    <div class="all box">
                        <h3>Display <strong><span class="green">All Members</span></strong></h3>
                        <p>All Members Displayed</p>
                    </div><!-- all box -->

                    <div class="name box">
                        <h3>Display by <strong><span class="green">Member Name</span></strong></h3>
                        <p><form name="namesearch" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>"> 
                            Name:&nbsp;&nbsp;<input type="text" name="find">
                            <input type="submit" name="search" value="Search Names">
                        </form></p>
                    </div><!-- name box -->

                    <div class="type box">
                        <h3>Display by <strong><span class="green">Member Type</span></strong></h3>
                        <p><form action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post">
                            Type:&nbsp;&nbsp;<select name="type" id="type">
                            <option value="">-- Select A Type --</option>
                            <?php
                                $query = "SELECT * FROM select_type";   //create type drop-down menu
                                $result = mysqli_query($connection, $query); 
                                while ($line = mysqli_fetch_array($result)) { 
                                echo "<option value='". $line['type'] ."'>". $line['type']."</option>";
                                } 
                            ?> 
                            </select>
                            <input type="submit" name="searchType" value="Search Types">
                        </form>
                    </div><!-- type box -->

                </div><!-- end vector -->

            </div><!-- end service-->

            <div id="media" class="group">

                <div class="all_listing box">
                    <p>Directory Listing</p>
                    <!-- Start Directory Listing -->
                    <?php
                        $sql = "SELECT * FROM members ORDER BY name";   // Database query and results
                        $result = mysqli_query($connection, $sql);
                        while($row = mysqli_fetch_array($result)) {
                            // Check record for website
                            if ($row['web']!== "") {
                                echo "<a target=blank href=". $row['web'] . ">" .$row["name"] . "</a><br> " .
                                "". $row["type"] . "<br>" .
                                "Address: " . $row["physicaladdress"] . "<br>" .
                                "Phone: " . $row["phone"] . "<br>" . "<hr>";
                            }
                            else {
                                echo $row["name"]. "<br> " .
                                "". $row["type"] . "<br>" .
                                "Address: " . $row["physicaladdress"] . "<br>" .
                                "Phone: " . $row["phone"] . "<br>" . "<hr>";
                            }
                        }
                        $connection->close();
                    ?>
                </div><!-- all_listing box -->

                <div class="all_name box">
                    <p>Results based on Name Search</p>
                    <?php
                        if (isset($_POST['search'])) {  // Has "Select Names button ben pushed
                            $find = $_POST['find'];
                            $sql = "SELECT * FROM members WHERE name LIKE '%" . $find . "%' ";
                            $result = mysqli_query($connection, $sql);
                            if(! $result) {
                                die ('Could not access database: ' . mysqli_error());
                            }
                            while($row = mysqli_fetch_array($result)) {
                                // Check record for website
                                if ($row['web']!== "") {
                                    echo "<a target=blank href=". $row['web'] . ">" .$row["name"] . "</a><br> " .
                                    "". $row["type"] . "<br>" .
                                    "Address: " . $row["physicaladdress"] . "<br>" .
                                    "Phone: " . $row["phone"] . "<br>" . "<hr>";
                                }
                                else {
                                    echo $row["name"]. "<br> " .
                                    "". $row["type"] . "<br>" .
                                    "Address: " . $row["physicaladdress"] . "<br>" .
                                    "Phone: " . $row["phone"] . "<br>" . "<hr>";
                                }
                            }
                        }
                    ?>
                </div><!-- all_name box -->
    </body>
</html>

我将代码修改为推荐但仍然收到与上述相同的错误。这是我遇到困难的地区:

这是将在div 2中显示搜索选项的分区(div 1):

<div id="web">
    <div id="sort_options">
        <label><input type="radio" name="sortRadio" value="name"> Name</label><br />
    </div><!-- sort_options -->
</div><!-- end web -->

这是div 2的搜索功能:

<div class="name box">
    <form name="namesearch" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>"> 
        Name:&nbsp;&nbsp;<input type="text" name="find">
        <input type="submit" name="search" value="Search Names">
    </form></p>
</div>

这是div 3中的php代码,用来自div 2的输入运行查询:

<div class="all_name box">
<?php
if (isset($_POST['search'])) 
{   
    $find = $_POST['find'];
    $sql = "SELECT * FROM members WHERE name LIKE '%" . $find . "%' ";
    $result = mysqli_query($connection, $sql);

    if ( $result == false ) 
    {
        echo ("Error description: " . mysqli_error($connection));
    }
    else
     {
        while($row = mysqli_fetch_array($result))
        {
            // Check record for website
            if ($row['web']!== "") 
            {
                echo "<a target=blank href=". $row['web'] .">" .$row["name"] . "</a><br> " .
                "". $row["type"] . "<br>" .
                "Address: " . $row["physicaladdress"] . "<br>" .
                "Phone: " . $row["phone"] . "<br>" . "<hr>";
            }
            else
            {
                echo $row["name"]. "<br> " .
                "". $row["type"] . "<br>" .
                "Address: " . $row["physicaladdress"] . "<br>" .
                "Phone: " . $row["phone"] . "<br>" . "<hr>";
            }
        }
    }
}
    ?>
</div>

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

更改此代码段:

' '+

至少

$sql = "SELECT * FROM members ORDER BY name";   // Database query and results
$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_array($result)) {

原因:如果您的查询有问题,$sql = "SELECT * FROM members ORDER BY name"; // Database query and results $result = mysqli_query($connection, $sql); while($result !== false && $row = mysqli_fetch_array($result)) { 将使用值$result,这将导致给定错误。

更好的代码就是这样:

false

一如既往:以错误为标准,成功是例外!