【问题标题】:How to display one item in my item details page from my item page using PHP/MYSQL如何使用 PHP/MYSQL 从我的项目页面在我的项目详细信息页面中显示一项
【发布时间】:2021-07-29 18:43:00
【问题描述】:

好的,所以在这里我正在尝试将项目详细信息页面链接到项目页面,我已经成功地做到了,但是当我点击一个项目时,所有项目详细信息都显示出来我想知道如何只显示一个对应 id 的商品详情页面上的商品。我知道我缺少一些代码(认为它是 GET 或 POST 变量)来完成链接,如果有人可以提供帮助,将不胜感激,非常感谢

这是我的代码

item.php

            <div class="rone">
            <?php
        $conn = new mysqli("xxx", "xxx", "xxx", "xxx");
        if ($conn->connect_error) {
            die("Connection Failed!" . $conn->connect_error);
        }
        ?>
            <?php
                $stmt = $conn->prepare('SELECT * FROM items');
                $stmt->execute();
                $result = $stmt->get_result();
                while ($row = $result->fetch_assoc()):
            ?>

            <div class="col-4">
                <a href="item-details.php?id=<?= $row['id'] ?>"><img src="<?= $row['itemimage'] ?>"></a>
                <h4"><?= $row['item_name'] ?></h4>
           </div>
           
            <?php endwhile; ?>                                                           
            </div>

item-details.php

            <div class="rone">
            <?php
        $conn = new mysqli("xxx", "xxx", "xxx", "xxx");
        if ($conn->connect_error) {
            die("Connection Failed!" . $conn->connect_error);
        }
        ?>
            <?php
                // UPDATE when this is done it does not work it gives the following error:: Uncaught Error: Call to a member function execute() on bool in C:\xampp\htdocs\assignment1\item-details.php:111 Stack trace: #0 {main} thrown in and the line 111 they referring to is this $stmt->execute(); ------ But if I do this //$id = $_GET['id'];
                //$stmt = $conn->prepare('SELECT * FROM items WHERE id = 1') the code will work just fine but it will only show item 1 regardless of which item I click. Hope this helps ????
                //OLD
                //$id = $_GET['id'];
                //$stmt = $conn->prepare('SELECT * FROM items WHERE id = $id');
                //$stmt->execute();
                //$result = $stmt->get_result();
                //while ($row = $result->fetch_assoc()):

               //UPDATED is this SQL Injection Proof?
                $id = $_GET['id'];
                $sql = "SELECT * FROM items WHERE id = ?";
                $stmt = $conn->prepare($sql); 
                $stmt->bind_param("i", $id);
                $stmt->execute();
                $result = $stmt->get_result();
                while ($row = $result->fetch_assoc()):


            ?>
            <div class="col-2">
                <img src="<?= $row['itemimage'] ?>" width="100%" id="ItemImg">                
            
            </div>
            <div class="col-2">
                <h1><?= $row['item_name'] ?></h1>
            </div>
<?php endwhile; ?>
    </div>

我的行的名称是: id 、 itemimage 、 item_name ,我将在以后添加其他属性 主表是项目。

【问题讨论】:

标签: php mysql mysqli


【解决方案1】:

我看到 $id ' ' ); 删除其中一个 ' 到 {$id}');

现在你已经正确了,现在你需要这样做

$sql = "SELECT * FROM items WHERE id = ?";
$stmt = $conn->prepare($sql); 
$stmt->bind_param("i", $id);
$stmt->execute();

在bind_param 上有"i" 什么"i" 是什么意思?这意味着 $id 的数字数据,如果 $id 是字符串,则应将其替换为“s”

不要忘记关闭 $stmt->close();在完成使用 $stmt 之后,这是最佳实践,因为我们不知道您是否在结束该语句之前使用了准备语句而不是结束它。

【讨论】:

  • 我的错是错字和同样的问题?
  • 感谢您的帮助,但它没有帮助,因为 here 的所有内容都是一样的>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-02
  • 2020-04-04
相关资源
最近更新 更多