0

我已经为我的页面编写了搜索代码,但是我希望它显示包含搜索条件的产品......目前它只显示以 cirteria 开头的结果。

这是我的代码:

<?php
$name = $_GET["q"];
if ($name <= "") {
  echo ("");
}
else {
  $con = mysql_connect("localhost", "cl49-XXX", "vogalcms");
  if (!$con) {
    die('Could not connect: line 513 ' . mysql_error());
  }
mysql_select_db("cl49-XXX", $con) or die("Unable to select database");   
$result = mysql_query("SELECT * FROM products WHERE `prodname` LIKE '$name%' ") or die('Error: Line 519 (' . mysql_error() . ')');
  mysql_select_db("cl49-vogalcms", $con) or die("Unable to select database");   


  while ($row = mysql_fetch_array($result)) {
    $prodID = $row['prodID'];
    $prodname = $row['prodname'];
    $catagory = $row['catagory'];
    echo "
     <div class='row-fluid portfolio-block'>
    <div class='span5 portfolio-text'>
    <img src='admin/userpics/$prodID.jpg' height='50' width='50' alt='' />
    <div class='portfolio-text-info'>
                                                <h4>$prodname</h4>
    <p></p>
        </div>
        </div>
    <div class='span5'>
<div class='portfolio-info'>
Product ID
                                                <span>$prodID</span>
        </div>
  <div class='portfolio-info'>
                                                catagory
                                                <span>$catagory</span>
 </div>
 </div>
  <div class='span2 portfolio-btn'>
  <a href='edit_product.php?q=$prodID' class='btn bigicn-only'><span>View/Edit</span></a>
    </div>
    </div> ";
  }
}
?>

我希望能够搜索 ca 并且不仅显示 CAr、CArpet 还显示 nesCAfe?

4

2 回答 2

2

只需将您的 where 子句更改为:

WHERE `prodname` LIKE '%$name%' 
于 2013-08-15T09:54:41.613 回答
1

将您的搜索查询更改为

SELECT * FROM products WHERE `prodname` LIKE '%$name%'
于 2013-08-15T09:55:19.283 回答