我如何获得此PHP搜索代码以显示与输入类似的字段?

问题描述 投票:0回答:1

此循环在此显示我数据库中的所有条目:

require 'includes/dbh.inc.php';

 $query = isset($_GET['query']);
    // gets value sent over search form

    //$min_length = 1;
    // you can set minimum length of the query if you want but I keep it at zero to avoid issues

    //if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

        $query = htmlspecialchars($query);
        // changes characters used in html to their equivalents, for example: < to &gt;

        $query = mysqli_real_escape_string($conn ,$query);
        // makes sure nobody uses SQL injection

        $results = mysqli_query($conn, "SELECT * FROM piecedetailtbl WHERE (`ArtName` LIKE '%$query%') OR (`ArtistName` LIKE '%$query%')  OR (`Description` LIKE '%$query%')  OR (`ArtType` LIKE '%$query%')
            OR (`ArtMedium` LIKE '%$query%') OR (`ArtSubject` LIKE '%$query%') OR (`ArtStyle` LIKE '%$query%')" ) or die(mysqli_error($conn));

            if(mysqli_num_rows($results) > 0){ // if one or more rows are returned do following

            while($row = mysqli_fetch_array($results)){
            // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop

            echo "<div class='galleryitem'>";
            echo "<a target='_blank' href='" .$row['ArtImage']."'>";
            echo "<img src='uploads/" .$row['ArtImage']."'>" . "<br>";
            echo "</a>";
            echo $row['ArtName'] . "<br>" ;
            echo $row['ArtistName'] . "<br>";
            echo $row['Description'] . "<br>";
            echo $row['ImageSize'] . "<br>";
            echo "$".$row['ArtPrice'] ;
            echo "</div>";
                // posts results gotten from database
            }

        }
        else{ // if there is no matching rows do following
            echo "No results";
        }

我想做的是使它仅显示类似于此搜索框中的查询输入的条目:

<div class="search">
  <form action="search.php" method="POST">
   <input type="text" name="query" placeholder="Search">
       <input type="submit" value="Search">Go</button>
   </form>
</div>

但是当我按回车键时,它所做的只是向我显示数据库中的所有条目,就像在做“选择*从piecedetailtbl”

有人有什么建议吗?还是注意到我无法提供的错误?

php html search mysqli
1个回答
0
投票

为什么您的php是get方法而您的html是post?在搜索中认为获取更好:

<div class="search">
<form action="search.php" method="get">
<input type="text" name="query" placeholder="Search">
   <input type="submit" value="Search" name="query">Go</button>
 </form>
</div>

在php中也使用GET,它将起作用:)

© www.soinside.com 2019 - 2024. All rights reserved.