为每个特定项目创建一个产品页面 [英] Creating a product page for each specific item

查看:23
本文介绍了为每个特定项目创建一个产品页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Html、CSS、JS、PHP 等在我的第一个网站上工作.我想创建一个产品页面,买家可以在其中查看产品.然后,通过单击产品,买家将被重定向到该产品的页面,该页面将显示该产品的所有信息.我的项目存储在 MySql 数据库中.我的问题是,我是否必须为每个产品创建一个单独的页面?这是我目前拥有的用于更多上下文的代码.

 

<?php$connect = mysqli_connect('localhost', 'root','', 'cart');$query = 'SELECT * FROM products ORDER by id ASC';$result = mysqli_query($connect, $query);如果($结果):如果(mysqli_num_rows($result)>0):while($product = mysqli_fetch_assoc($result)):?><div class="col-sm-4 col-md-3"><form method="post";action=index.php?action=add&id=<?php echo $product['id']?>"><div class="products"><img src="<h4><?php echo $product['price'];?></h4><button><a href="<?php echo $product['redirect']?>">Buy</a></button>

</表单>

<?php终了;万一;万一;?>

如您所见,重定向按钮会将用户重定向到产品页面.我是否必须为每个产品创建一个单独的页面?请注意,这是我第一次在网站上尝试,所以请原谅我对这个主题的无知.提前致谢!

解决方案

不,您不必为每个产品创建一个新页面.您可以尝试为概览中的每个产品提供如下链接:

然后创建一个页面.此页面充当所有产品的占位符.我们将其称为产品页面.在产品页面上,您可以像这样获取此特定产品的数据:

$query = 'SELECT * FROM products WHERE id = "'.$_GET['product'].'"';

所以现在每个产品都有自己的页面,上面的查询获取当前产品的所有数据.

注意

我没有在这里使用准备好的语句.这只是一个关于如何获得产品的小演示.我强烈建议您使用它们!否则,您的网站会受到 SQL 注入!

I am working on my first website using Html, CSS, JS, PHP etc. I want to create a product page where the buyer would view the products. Then, by clicking on the product the buyer would be redirected to the page of that product where all the information of the product will be displayed. My items are stored in a MySql database. My question is, do I have to create a separate page for each product? Here is the code I currently have for more context.

        <div class="container">
    <?php

        $connect = mysqli_connect('localhost', 'root','', 'cart');
        $query = 'SELECT * FROM products ORDER by id ASC';
        $result = mysqli_query($connect, $query);

            if ($result):
                if(mysqli_num_rows($result)>0):
                    while($product = mysqli_fetch_assoc($result)):
                    ?>
        <div class="col-sm-4 col-md-3">
            <form method="post" action="index.php?action=add&id=<?php echo $product['id']?>">
                <div class="products">
                    <img src="<?php echo $product['image'];?>" class="img-responsive" />
                    <h4 class="text-muted">
                        <?php echo $product['name'];?>
                    </h4>
                    <h4><?php echo $product['price'];?></h4>
                    <button><a href="<?php echo $product['redirect']?>">Buy</a></button>
                </div>
            </form>
        </div>
        <?php
                    endwhile;
                endif;
          endif;
            ?>
</div>

As you can see the redirect button will redirect the user to the page of the product. Do I have to create a separate page for each product? Please note that this is my first ever attempt on a website so excuse my ignorance on the subject. Thanks in advance!

No, you don't have to crate a new page for each product. You could and try to give every product in your overview a link like this:

<a href="view_product.php?product=<?php echo $product['id'] ?>">Link to product</a>

Then create one page. This page acts as a placeholder for all the products. We'll call this the product page. On the product page, you can get the data for this specific product like this:

$query = 'SELECT * FROM products WHERE id = "'.$_GET['product'].'"';

So now every product has its own page and the query stated above gets all the data on the current product.

Note

I'm not using prepared-statement here. This only a small demo on how to get the product. I would strongly advise you to use them! Otherwise, your website is exposed to an SQL-injection!

这篇关于为每个特定项目创建一个产品页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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