搜索框无法显示结果,即使关键字与php中的数据库匹配,也始终显示无结果

时间:2014-03-09 12:24:16

标签: php html mysql

我有一个搜索框,结果将显示在同一表格上。

问题是我不知道为什么结果总是“无结果”,即使关键字应匹配我的数据库中的某些记录,并且表和列的名称是正确的。这是我的代码:

<html>
<Title>search </title>
<Body>
<form action = " " method = "POST" method = "GET">
<font  size  = 7 face = "arial rounded MT bold">
WELCOME to VPMG Tradings
</font>

<p align = left>
Enter product name or bar code : <input type = "text" name = "search" > <input type ="submit" name = "searched" value = "Search">
</align>
<br>

<table border = 5 align = center >
<tr><th>Barcode  </th><th>Item name </th><th>Description</th><th>Amount</th><th>Stock</th><th>Location</th>
</tr>
<?php
mysql_connect( "localhost" , "admin" , "123") or die(mysql_error());
mysql_select_db("minimart_database") or die(mysql_error());
$output = "";
if (isset ($_POST ["search"] )) {
        $searchq = $_POST ["search"];
        $searchq = preg_match("/[A-Z  | a-z]+/","", $searchq);
        $result ="SELECT * FROM stock WHERE ( barcode  = '%". $searchq ."') OR (itemname =  '% ".$searchq ."')";
        $query = mysql_query ($result) or die (mysql_error ());
        $count = mysql_num_rows ($query);
        if ($count ==0){
        $output = 'no results';
        }
        else{
        while ($row = mysql_fetch_array ($query)){
        $barcode = $row['barcode'];
        $itemname = $row['itemname'];   
        $description = $row['description'];
        $amount = $row['amount'];
        $stock = $row['stocks'];
        $location = $row['location'];

        $output = '<div> '.$barcode.' '.$itemname.' '.$description.' '.$amount.' '.$stock.' '.$location.' </div> ';
}
}
}

?>
<?php print ("$output"); ?>
 </table>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

$searchq = preg_match("/[A-Z  | a-z]+/","", $searchq);

将字符串设为零。 使用mysql_real_escape_string($searchq);

以及查询where子句使用like而不是=

Sugesstion:使用mysqli / PDO,因为不推荐使用mysql

答案 1 :(得分:0)

有时需要对代码进行硬编码才能使其正常工作。我使用此代码使我的搜索框工作,它正在工作:

<?php
mysql_connect( "localhost" , "admin" , "123") or die(mysql_error());
mysql_select_db("minimart_database") or die(mysql_error());
?>

<form id="home_id" method ="POST"  enctype ="multipart/form-data">
<script>
function submitForm(action)
{
document.getElementById('home_id').action=action;
document.getElementById('home_id').submit();
}
</script>
<p align=left><input type="text" name="search" placeholder="enter barcode or item name"><input type="submit" name="searched" onclick="submitForm('finalhome.php')">
<?php
$output="";
if (isset($_POST['search'])){
$searchq=$_POST['search'];
$searchq=mysql_real_escape_string($searchq);
$order="SELECT * FROM stock WHERE barcode LIKE '%$searchq' OR itemname LIKE  '%$searchq'";
$result=mysql_query($order);
$count=mysql_num_rows($result);

if ($count>=1){
        while($row=mysql_fetch_array($result)){
                $barcode=$row['barcode'];
                $itemname=$row['itemname'];
                $description=$row['description'];
                $amount=$row['amount'];
                $stocks=$row['stocks'];
                $location=$row['location'];
        }
            $output="<div>'.$barcode.' '.$itemname.' '.$description.' '.$amount.' '.$stocks.' '.$location.' </div>"; 
            }
            else
            {
            $output='no results';
            }
     }  
?>

<?php print("$output"); ?>