【问题标题】:get product data from database on clicking product tile单击产品图块从数据库中获取产品数据
【发布时间】:2020-04-29 04:14:08
【问题描述】:

所以我使用'while'在我的主页上显示产品,我希望当用户单击产品磁贴时,它会被重定向到包含有关该特定产品的所有详细信息的文件 product_page.php 我不知道是不是因为我使用了“a”标签,但它不起作用

<?php
include 'config.php';
$stmt = $conn->prepare("SELECT * FROM product");
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()):
?> 
<div class="col-sm-6 col-md-4 col-lg-3 mb-2" > 
<a href="product_page.php">
<div class="card-deck" id="product"> 
<div class="card border border-light ">
<img class="card-img-top" src="<?= $row['product_image'] ?>" >
<div class="card-body border border-light" style="background-color: #ffffff">
  <h4 class="card-title text-center text-info"><?= $row['product_name'] ?></h4>
  <h5 class="card-text text-center text-danger">Rs. <?= number_format($row['product_price'],2) ?> 
</h5> 
</div>
<div class="card-footer border border-light" style="background-color: #ffffff">
<form action="" class="form-submit">
  <input type="hidden" class="pid" value="<?= $row['id']?>">
  <input type="hidden" class="pname" value="<?= $row['product_name']?>">
  <input type="hidden" class="pprice" value="<?= $row['product_price']?>">
  <input type="hidden" class="pimage" value="<?= $row['product_image']?>">
  <input type="hidden" class="pcode" value="<?= $row['product_code']?>">
  <button class="btn btn-block addItemBtn" style="background-color: #FE701E; color:#ffffff">Add To 
   Cart</button>
  </form>
</div>
</div>
</div>
</a>
</div>

<?php endwhile; ?>

这是我的 action.php 代码

if(isset($_POST['pid']))
{
$pid = $_POST['pid'];
$pname = $_POST['pname'];
$pprice = $_POST['pprice'];
$pimage = $_POST['pimage'];
$stmt = $conn->prepare("SELECT * FROM product WHERE pid=? LIMIT 1");
$stmt->bind_param("i",$pid);
$stmt->execute();
$pcount=mysqli_num_rows($stmt);
$res = $stmt->get_result();
$r = $res->fetch_assoc();
if($pcode>0)
{
$_SESSION['product_name'] =$r['product_name'];
$_SESSION['product_price'] =$r['product_price'];
$_SESSION['product_image'] =$r['product_image'];
header("Location:product_page.php");
}

这里是ajax请求

$(document).ready(function(){
  $("#product").click(function(e){
    e.preventDefault();
    var $form =$(this).closest(".form-submit");
    var pid = $form.find(".pid").val();
    var pname = $form.find(".pname").val();
    var pprice = $form.find(".pprice").val();
    var pimage = $form.find(".pimage").val();
    var pcode = $form.find(".pcode").val();

    $.ajax({
      url:'action.php',
      method:'post',
      data:{pid:pid,pname:pname,pprice:pprice,pimage:pimage,pcode:pcode},
      success:function(response){
                 }
    });
  });});

【问题讨论】:

  • 回复 #product - 这可能会有所帮助 does id have to be unique
  • 我使用的id是唯一的
  • 您有多个问题:1)#product 用于循环,这使得它重复且不唯一。最好使用class="product" data-id="&lt;?= $row['id'] ?&gt;" 2) 非破坏性(读取请求)只能是 GET。无需表格或重定向。只需在 Get 请求中发送 ID。 3)您的ajax(如果您选择使用它)对成功没有任何作用。您更改位置的标头仅意味着它将 product_page.php 返回到调用 ajax 函数。据我所知,您甚至不想要这里的 ajax 功能。你希望它重定向。

标签: php html mysql ajax


【解决方案1】:

您的方法令人困惑。为什么你有一个指向 products.php 的链接?您可以尝试以下方法之一

选项 1 如果您不想在 URL 中看到除 ID 之外的任何数据,这是一个更好的选择。 仅通过 products.php 的链接传递 ID。在那里,您获取所需的其余数据。

<a href="products.php?id=<?php echo $row['id'];?>Link text here, which could be the ID</a>

将它放在您的循环中并在products.php 中,您只需根据链接中的 ID 查询数据库,即可在该页面中获取您需要的数据。

选项 2 这可能更容易,因为您已经拥有所有数据,除非您不希望将某些数据显示为 URL 的一部分) 将您的网址中的所有这些变量作为GET 参数传递

echo '<a href="products.php?id=' . $row['id'] . '&pname= . $row['product_name'] . '>Link text here</a>"

使用&amp; 运算符继续添加所有参数。

【讨论】:

    【解决方案2】:

    您正在#product 上调用 ajax 请求。当您在循环中展示产品时,#product 并不是唯一的。 html 的任何元素的ID 在同一页面中都应该是唯一的。我建议您将 id 提供给 &lt;form&gt; 而不是 &lt;div&gt; 所以您可以创建一个唯一的 id,例如 -

    <form action="" class="form-submit" id="product_<?php echo $row['id']; ?>"> 
    

    然后您可以调用 ajax 请求。 -

    $(document).on('submit', '[id^="product_"]', function(){
      e.preventDefault();
        var $form =$(this).closest(".form-submit");
        var pid = $form.find(".pid").val();
        var pname = $form.find(".pname").val();
        var pprice = $form.find(".pprice").val();
        var pimage = $form.find(".pimage").val();
        var pcode = $form.find(".pcode").val();
    
        $.ajax({
          url:'action.php',
          method:'post',
          data:{pid:pid,pname:pname,pprice:pprice,pimage:pimage,pcode:pcode},
          success:function(response){
                     }
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-30
      • 1970-01-01
      • 2018-03-31
      • 1970-01-01
      • 2021-02-21
      • 1970-01-01
      相关资源
      最近更新 更多