PHP网站搜索和显示项目 [英] PHP website search and display items

查看:37
本文介绍了PHP网站搜索和显示项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个虚拟的在线商店来说明一些现实世界的功能,其中之一是在网站上搜索商品,即

I am creating a dummy online store to illustrate some real world functionality which one of them is to search the website for items i.e.

我已经编写了 PHP 代码来处理这种情况,但它无法正常工作.它所做的事情是它与结果和结果数量相匹配,但它没有显示它们,我当然不会这样做.

I have written PHP code to deal with this scenario but it does not work properly. Wchat it does dough is it matches the results and the number of results but it does not display them which I of course wont it to do.

一直在尝试在 GOOGLE 上寻找答案,但没有找到相应的解决方案或针对我的问题的提示.

Been trying to look for answers on GOOGLE but didn't find corresponding solution or a tip to my problem.

这里将列出我正在使用的代码:

Here am gonna list the code I am using:

PHP 代码(search.php):

PHP code (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):

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>

当前结果的打印屏幕:

正如您所注意到的,它还说找到了 3 个结果,这是正确的,因为我一直在搜索这是我的产品的通用名称,但只有两张桌子而且它们是空的.

As you have have noticed it also says 3 results have been found which is correct considering i have searched for ever which is a common name of my product but drows up only two tables moreover they are empty.

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

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

finally my product table consists of : product_id product_name product_qua product_price product_image product_des product_type attrebiutes/columns

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

anyone can spot where i might be going wrong with this....?

推荐答案

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

  1. mysql_fetch_row( $query_search) returns a plain array not an associative array but you are trying to access its values using keys - $query_row [ 'product_name' ]. Rather use _fetch_array

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

There are lots of syntactical errors. There is a space between function name and list of parameters.

不要使用mysql_.它们已被弃用(阅读:已死).改用 PDO.

Don't use mysql_. They are deprecated (read: dead). Use PDO instead.

这篇关于PHP网站搜索和显示项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆