[英]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.