我正在尝试建立一个网站,但我的通配符搜索查询一直给我提出问题。
它没有显示任何错误,但结果显示为空白。 您可以在此网站上查看它:luke.dx.am/Search.php
她是我搜索栏的代码。
<!-- Custom Search -->
<form action="Search.php" method="post">
<input type="text" name="search" placeholder="Search"/>
<input type="submit" value=">>"/>
</form>
这是执行搜索查询的代码。
<?php
$output = '';
$searchq = '';
$search_query = '';
$search_results = '';
$result = '';
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
//Query for the search engine
$search_query = "SELECT Item.Item, Category.Category, Colour.Colour,
Description.Description, products.Price, products.Stock, products.date,
Sexs.Sex, Sizes.Size, Designers.Designer, Designers.Email,
Designers.Phone, Designers.Address, Image.Image
FROM products
INNER JOIN Category
ON Category.CatagoryCode=products.CatagoryCode
INNER JOIN Colours
ON Colour.ColourCode=products.CatagoryCode
INNER JOIN Description
ON Description.DescriptionCode=products.DescriptionCode
INNER JOIN Designers
ON Designers.DesignerCode=products.DescriptionCode
INNER JOIN Sexs
ON Sexs.SexCode=products.SexCode
INNER JOIN Image
ON Image.ImageCode=products.ItemCode
INNER JOIN Item
ON Item.ItemCode=products.ItemCode
INNER JOIN Sizes
ON Sizes.SizeCode=products.SizeCode
WHERE Item.Item LIKE '%$searchq%' OR Category.Category LIKE '%$searchq%'
OR Colour.Colour LIKE '%$searchq%' OR Description.Description LIKE
'%$searchq%' OR Sexs.Sex LIKE '%$searchq%' OR Sizes.Size LIKE
'%$searchq%' OR Designers.Designer LIKE '%$searchq%'";
$search_results=mysqli_query($con, $search_query);
if($result == FALSE) {
?><p>Error</p><?php
die(mysql_error()); // TODO: better error handling
}else{
//Runs in a while loop
while($output=mysqli_fetch_array($result))
{
?>
对于绝对的代码墙感到抱歉。
答案 0 :(得分:1)
您需要更正代码。您在$ search_results中获得结果,然后在$ result中检查它。
$search_results=mysqli_query($con, $search_query);
if($result == FALSE) { ...
需要
$search_results=mysqli_query($con, $search_query);
if($search_results == FALSE) {...
答案 1 :(得分:0)
首先在提交按钮上应用名称。
<input name="submit" type="submit" value=">>"/>
第二次检查是否设置(点击)然后处理表格。
<?php
$search_results=mysqli_query($con, $search_query);
if(isset($_POST['submit'])){
检查这样的结果不需要使用$ result == FALSE。的!就像说不(如果不是结果)
if(!$search_results){
?><p>Error</p><?php
die(mysql_error()); // TODO: better error handling
}else{
//在while循环中运行
while($output= $search_results->fetch_assoc()) {
$outputA = $output['tableNamea'];
$outputB = $output['TanbeNameb'];
}
整件事
<?php
$search_results=mysqli_query($con, $search_query);
if(isset($_POST['submit'])){
if(!$search_results){
?><p>Error</p><?php
die(mysql_error()); // TODO: better error handling
}else{
while($output= $search_results->fetch_assoc()) {
$outputA = $output['tableNamea'];
$outputB = $output['TanbeNameb'];
}
?>