【问题标题】:Display item details after clicking on element单击元素后显示项目详细信息
【发布时间】:2018-07-31 08:58:25
【问题描述】:

我正在做一个项目,我们在数据库中有一些项目显示在网站上,并且运行良好的代码如下:

<form method="post" action="store.php?action=add&ItemNo=<?php echo $products['ItemNo']; ?>">
    <a href="product.php"><div class="products">
        <img src="<?php echo $products['img'];?>" class="img-responsive"/>
        <!--  this connects to the database and gets the image of item    -->
        <h4><?php echo $products['ItemName'];?></h4>
        <h3>$ <?php echo $products['Price'];?></h3> 
        <input type="text" name="Quantity" class="form-control" value="1"/>
        <!--  This creates an box where the default qnt is set to 1    -->
        <input type="hidden" name="ItemName" value="<?php echo $products['ItemName']; ?>" />
        <input type="hidden" name="Price" value="<?php echo $products['Price']; ?>" />
        <input type="submit" name="addtocart" style="margin-top:5px;" class="btn btn-info"
               value="Add to Cart"/>
        <!--   this creates the btn that adds item to cart   -->
        </div>
    </a>
</form>

现在我正在尝试展示有关每种产品的更多详细信息。我想要的只是当用户单击任何项​​目时,他们将被发送到item.php 页面,其中将有关于该特定项目的更多详细信息。

此时我在 item.php 中显示了所有信息,但我被卡住了。

我不知道如何表达这个问题,也许这就是我在 Google 上找不到任何帮助的原因。我是 PHP 新手,因为我几周前才开始接触它。

【问题讨论】:

  • 你有一个带有列表的页面和一个带有特定的页面吗?您需要一种技术将其组合成一个结果。您可以立即显示一个弹出窗口(我不推荐),将数据上传到 iframe(我也不推荐),发送带有 id 的整个表单(不带排序选项),显示详细信息(特别不推荐),立即加载数据并通过几个(例如jQuery ('#id-product') show())显示它们(我不推荐大量结果列表)或使用AJAX(甚至websocket)加载数据。从编程科学的角度来看,你应该练习所有的方法。

标签: php html css mysql


【解决方案1】:

首先

<a href="product.php?id=<?php echo $products['ItemNo']; ?>"> 

然后在 product.php 上检查 $_GET['id'] 的 url 并在您的查询中使用它。

SELECT * from foo where id=$id

但不要使用原始 id 或非参数化查询,除非您喜欢被黑客入侵

【讨论】:

  • @DawidGodawa 首先更改连接到 PDO 驱动程序(和 prepare)的方式,或者使用像 database query builder 这样的库
  • 相当多的东西被称为“查询构建器”你有什么特别的想法吗? @bato3
  • 我喜欢 kohana 中使用的解决方案,但我不想将解决方案强加于任何人。 (使用准备好的查询除外)docs.koseven.ga/guide/database/query/builder
  • 我想到的是当用户点击任何项目时,新页面打开时会显示项目详细信息,例如 echo $products['ItemName'] 等。
  • @DawidGodawa 这就是我刚刚回答的?我的回答有什么不清楚的地方?
猜你喜欢
  • 1970-01-01
  • 2017-06-08
  • 2019-11-09
  • 2020-06-28
  • 1970-01-01
  • 2020-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多