繁体   English   中英

如何使用 PHP/MYSQL 从我的项目页面在我的项目详细信息页面中显示一个项目

[英]How to display one item in my item details page from my item page using PHP/MYSQL

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

这是我的代码

项目.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>

项目详细信息.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 👍

                $id = $_GET['id'];
                $stmt = $conn->prepare('SELECT * FROM items WHERE id = $id');
                $stmt->execute();
                $result = $stmt->get_result();
                while ($row = $result->fetch_assoc()):
            ?>
            <div class="col-2">
                <img src="<?= $row['itemimage'] ?>" width="100%" id="ItemImg">                
            <?php endwhile; ?>
            </div>
            <div class="col-2">
                <h1><?= $row['item_name'] ?></h1>
            </div>
    </div>

我的行的名称是: id 、 itemimage、 item_name 并且我会及时添加其他属性并且主表是项目。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM