我創建一個虛擬的網上商店,以說明一些真實世界的功能,這其中的一個是搜索網站的項目,即PHP網站的搜索和顯示項目
我已經寫PHP代碼來處理這種情況,但它不能正常工作。 Wchat它做麪糰是匹配的結果和結果的數量,但它不顯示他們,我當然不會做它。
一直試圖在GOOGLE上尋找答案,但沒有找到相應的解決方案或提示我的問題。
我在這裏會名單我使用的代碼:
PHP代碼(search.php中):
<?php
session_start();
include('connect_mysql.php');
$product_name = 'product_name';
$product_qua = 'product_qua';
$product_price = 'product_price';
$product_image = 'product_image';
$product_des = 'product_des';
if (isset($_POST['keyword']))
{
$search = $_POST['keyword'];
if (!empty($search))
{
$query = "SELECT product_name FROM products WHERE product_name='$search'";
$query_search = mysql_query($query);
echo mysql_num_rows($query_search);
if (mysql_num_rows($query_search) >=1)
{
echo 'Results found: <br>';
while ($query_row = mysql_fetch_row($query_search))
{
echo $query_row['product_name'];
}
while($rows = mysql_fetch_array($query_search))
{ ?>
<table id='display'>
<tr><td><?php echo "<img src=$rows[$product_image] class='grow'>" ?></td></tr>
<tr>
<th></th>
<th><strong>Avalible</strong></th>
<th><strong>Price</strong></th>
<th><strong>Description</strong></th>
</tr>
<tr>
<td width='290px'><?php echo "$rows[$product_name]" ?></td>
<td width='290px'><?php echo "$rows[$product_qua]" ?></td>
<td width='290px'><?php echo "£ $rows[$product_price]" ?></td>
<td width='290px'><?php echo "$rows[$product_des]" ?></td>
</tr>
<tr>
<td><p>Please Login To purchase this item </p><br /><a href="login.php">Login</a></td>
</tr>
</table>
<?php
}
} else {
echo 'NO results found.';
}
}
}
?>
HTML代碼(的index.php):
<form action="search.php" method="post">
<input type="text" name="keyword" size="20" placeholder="Search for products...."/>
<input type="submit" value="Search >>" />
</form>
打印屏幕當前結果:
正如你已經注意到它也說3個結果已被發現,這是正確的,考慮到我已經搜索過,這是我的產品的常見名稱,但只淹沒兩個表,而且他們是空的。
網址:http://studentnet.kingston.ac.uk/~k1024026/index.php
終於我的產品表包括:PRODUCT_ID PRODUCT_NAME product_qua PRODUCT_PRICE product_image product_des PRODUCT_TYPE attrebiutes /列
任何人都可以當場在那裏我可能與此是想錯了....?
好的thx爲您的答覆,但我將如何實施您所建議的更改。 – Tomazi 2013-03-12 02:17:30
,爲什麼mysql_fetch_row($ query_search)返回一個普通數組......? – Tomazi 2013-03-12 02:20:22
@Tomazi你是什麼意思 - '_why是否mysql_fetch_row($ query_search)返回一個普通的array_'?這是因爲開發商已經這樣決定了。閱讀PDO上的PHP手冊。 – ShuklaSannidhya 2013-03-12 02:34:56