如何在查询中搜索空白

时间:2017-09-13 02:09:15

标签: php html mysqli

当我转到此页面时,在选择选项中的值之前,它已经在变量中搜索空白字符串。我只想在按钮单击时搜索值,而不是在用户转到此页面时自动搜索。此外,我的“asc”在查询中也存在问题。

如果我转到此页面。它已经搜索并显示空白值 Already Search the Blank

我插入此代码$asc,但正如您在图片中看到的那样,它会显示错误,但如果我选择了选择选项,则错误将消失并显示该值。 this Ascending to Descending

    请在下面输入详细信息

    <div class="nice"  style=" width:20%; margin-left:10%; z-index:100px;">
<div class="control-group">
        <label class="control-label" for="inputPassword">Search Catalog:</label>
        <div class="controls">
        <select name="catalogs">
        <option></option>
        <?php $result =  mysqli_query($dbcon,"select * from book_catalog")or die(mysqli_error()); 
        while ($row=mysqli_fetch_array($result)){ ?>
        <option value="<?php echo $row['catalog']; ?>"><?php echo $row['catalog']; ?></option>
        <?php } ?>
        </select>
        </div>
    </div>


    <div class="control-group">
        <label class="control-label" for="inputPassword">Author:</label>
        <div class="controls">
        <select name="status">
            <option></option>
            <option>Local</option>
            <option>Foreign</option>
        </select>
        </div>
    </div>
    <div class="control-group" style="margin-left:210%; margin-top:-150px;">
        <label class="control-label" for="inputPassword">Ascending-Descending</label>
        <div class="controls">
        <select name="asc" >
            <option></option>
            <option value="acc_id">Accession Number</option>
            <option value="author">Author</option>
            <option value="book_title">Book Title</option>
            <option value="publisher_name">Publisher Name</option>
            <option value="copyright_year">Copyright Year</option>
            <option value="date_added">Date Added</option>
        </select>
        </div>
    </div>
    </div>  

    <input type="submit" name="submit" value="Search" style="margin-left:43%; margin-top:10%;" /> <br>

     </form>


        </div>



        <?php
        $selected_stat = "";
        $selected_val = ""; 
        $asc ="";  // Storing Selected Value In Variable

        if(isset($_POST['submit'])){
        $selected_val = $_POST['status'];
        $selected_stat = $_POST['catalogs']; 
        $asc = $_POST['asc'];  // Storing Selected Value In Variable

      }
     ?>

   <br>

                            <thead>
                                <tr>
                                <th>Acc No.</th>   
                                <th>Call Number</th> 
                                <th>Author</th>   
                                <th>Author Category</th>                          
                                <th>Book Title</th>                                 
                                 <th>Category</th>
                                <th>Book Copies</th>
                                <th>Publisher Name</th>
                                <th>Copyright Year</th>
                                <th>Date Added</th>
                                <th class="action">Action</th>


                                </tr>
                            </thead>
                            <tbody>

                              <?php  $user_query=mysqli_query($dbcon,"select * from book where catalog = '$selected_stat' OR status = '$selected_val' ORDER BY book.$asc ASC")or die(mysqli_error($dbcon));
                                while($row=mysqli_fetch_array($user_query)){
                                $id=$row['book_id'];  
                                ?>
                                <tr class="del<?php echo $id ?>">
                                <td><?php echo $row['acc_id']; ?></td>
                                <td><?php echo $row['callnum']; ?></td>
                                <td><?php echo $row['author']; ?> </td> 
                                <td><?php echo $row['status']; ?></td>
                                <td><?php echo $row['book_title']; ?> </div></td>
                                <td><?php echo $row ['catalog']; ?> </td>
                                <td class="action"><?php echo $row['book_copies'];   ?> </td>
                                 <td><?php echo $row['publisher_name']; ?></td>
                                 <td><?php echo $row['copyright_year']; ?></td>     
                                 <td><?php echo $row['date_added']; ?></td>
                                 <?php include('toolttip_edit_delete.php'); ?>
                                <td class="action">
                                    <a rel="tooltip"  title="Delete" id="<?php echo $id; ?>" href="#delete_book<?php echo $id; ?>" data-toggle="modal"    class="btn btn-danger"><i class="icon-trash icon-large"></i></a>
                                    <?php include('delete_book_modal.php'); ?>
                                    <a  rel="tooltip"  title="Edit" id="e<?php echo $id; ?>" href="edit_book.php<?php echo '?id='.$id; ?>" class="btn btn-success"><i class="icon-pencil icon-large"></i></a>

                                </td>   

                                </tr>
                                <?php  }  ?>

                            </tbody>
                        </table>

抱歉我的英文不好

2 个答案:

答案 0 :(得分:0)

像这样进行查询

mysqli_query($dbcon,"select * from book where catalog = '" . $selected_stat ."' OR status = '" . $selected_val ."' ORDER BY book." . $asc . " ASC")or die(mysqli_error($dbcon));

答案 1 :(得分:0)

当SQL语句抛出错误时,最好的办法是将其记录或转储到屏幕上,以便将其视为实际的SQL。鉴于声明的形式,我认为$ asc不是你认为的,它甚至可能是空白的:

ORDER BY book. ASC

这本书。是数据库无法处理的事情。

我认为在加载时自动运行查询的页面的问题是因为您直接在select元素中调用查询

<select name="catalogs">
    <option></option>
    <?php $result =  mysqli_query($dbcon,"select * from book_catalog")or die(mysqli_error()); 
    while ($row=mysqli_fetch_array($result)){ ?>
    <option value="<?php echo $row['catalog']; ?>"><?php echo $row['catalog']; ?></option>
    <?php } ?>
    </select>

PHP代码不等待用户输入,它只是立即运行。