0
作爲問題,它只拾取了我的product_name中第一個以var_char(50)作爲主鍵的單詞。Php搜索腳本只拾取第一個字
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
$searchquery = $_POST['searchquery'];
if($_POST['filter1'] == "Whole Site"){
$sqlCommand = "SELECT * FROM products WHERE MATCH (product_name ,details,category,subcategory) AGAINST ('".$searchquery."' IN BOOLEAN MODE)";
}
require_once("storescripts/connect_to_mysqli.php");
$query = mysqli_query($myConnection,$sqlCommand) or die(mysqli_error($myConnection));
$count = mysqli_num_rows($query);
if($count > 1){
$search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />$sqlCommand<hr />";
while($row = mysqli_fetch_array($query)){
$id=$row["id"];
$product_name = $row["product_name"];
$details= $row["details"];
$category=$row["category"];
$subcategory=$row["subcategory"];
$search_output .= "ID: $id <br/> Name: $product_name -<br/>$details<br />$category<br/>$subcategory<br/>
<a href='product.php?id=$id'>link</a><br/>
";
} // close while
} else {
$search_output = "<hr />0 results for <strong>$searchquery</strong><hr />$sqlCommand";
}
}
?>
<html>
<head>
</head>
<body>
<h2>Search the Exercise Tables</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Search For:
<input name="searchquery" type="text" size="44" maxlength="88">
Within:
<select name="filter1">
<option value="Whole Site">Whole Site</option>
</select>
<input name="myBtn" type="submit">
<br />
</form>
<div>
<?php echo $search_output; ?>
</div>
</body>
這是由於它是一個主鍵?它實際上不工作的細節,以及,它其實很奇怪,不工作的大部分話,但工作some..i我完全confused..it不作任何感官
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once("storescripts/connect_to_mysqli.php");
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
$searchquery = $_POST['searchquery'];
if($_POST['filter1'] == "Whole Site"){
$sqlCommand = sprintf("SELECT * FROM products WHERE product_name LIKE '%s%%'",
mysqli_real_escape_string($myConnection, $searchquery));
}
$query = mysqli_query($myConnection,$sqlCommand) or die(mysqli_error($myConnection));
$count = mysqli_num_rows($query);
if($count > 1){
$search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />$sqlCommand<hr />";
while($row = mysqli_fetch_array($query)){
$id=$row["id"];
$product_name = $row["product_name"];
$details= $row["details"];
$category=$row["category"];
$subcategory=$row["subcategory"];
$search_output .= "ID: $id <br/> Name: $product_name -<br/>$details<br />$category<br/>$subcategory<br/>
<a href='product.php?id=$id'>link</a><br/>
";
} // close while
} else {
$search_output = "<hr />0 results for <strong>$searchquery</strong><hr />$sqlCommand";
}
}
?>
<html>
<head>
</head>
<body>
<h2>Search the Exercise Tables</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Search For:
<input name="searchquery" type="text" size="44" maxlength="88">
Within:
<select name="filter1">
<option value="Whole Site">Whole Site</option>
</select>
<input name="myBtn" type="submit">
<br />
</form>
<div>
<?php echo $search_output; ?>
</div>
</body>
</html>
我上面的代碼試圖以及並縮小到product_name,它仍然不顯示,例如我有這個product_name作爲'歐米加魚'輸入,如果我輸入'魚'它不顯示一件事,如果我鍵入'梅加'那麼它的作品
我已經給了這個和LIKE功能試用,它似乎更好,但仍然有趣..請參考此鏈接對於那些有興趣的人
PHP search script for mySQL database, only 3 letter working
感謝
你調試的是?你確定查詢本身提供了錯誤的結果(並且沒有問題,例如,在用戶輸入中)? –
如果$ _POST ['filter1']!=「Whole Site」,那麼$ sqlCommand爲空,因爲您沒有設置替代$ sqlCommand語句。 – jeff
你檢查了結果頁面的html源代碼嗎? – Cups