PHP网站搜索和显示项目

时间:2013-03-12 01:42:37

标签: php html mysql

我正在创建一个虚拟在线商店来说明一些真实世界的功能,其中一个是搜索网站上的项目,如

我编写了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 code(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>

当前结果的打印屏幕:

enter image description here

正如你已经注意到的那样,它也说已经找到了3个结果,这是正确的,因为我已经搜索了我的产品的常用名称,但只有两个桌子,它们都是空的。

网址网址:http://studentnet.kingston.ac.uk/~k1024026/index.php

最后我的产品表包含:product_id product_name product_qua product_price product_image product_des product_type attrebiutes / columns

任何人都可以发现我可能出错的地方....?

3 个答案:

答案 0 :(得分:0)

首先,尝试删除此

while ($query_row = mysql_fetch_row($query_search)) 
                {
                    echo $query_row['product_name'];

                }

我还注意到你的代码中有一些错误的错误:

  • table id =“display”一个id应该是唯一的。如果你迭代它并仍然希望它是一个id,那么改为display-n,n就是产品的唯一id。 (或使用class =“display”而不是id)

  • 你应该看看sql注入以及如何击败它们。

答案 1 :(得分:0)

我可能宁愿这样做:

$query = "SELECT product_name FROM products WHERE product_name LIKE %" . $search . "%";

希望它会有所帮助。然后使用foreach循环来运行结果,如:

foreach ($search as $key => $result){
      echo $result . '<br />';
}

答案 2 :(得分:0)

  1. mysql_fetch_row( $query_search)返回一个普通数组,而不是关联数组,但您尝试使用键 - $query_row [ 'product_name' ]访问其值。而是使用_fetch_array

  2. 有很多语法错误。函数名和参数列表之间有一个空格。

  3. 请勿使用mysql_。它们已被弃用(读:已死)。请改用PDO。