php - 单击 PHP 中的项目时如何获取更多信息?
问题描述
我第一次使用 PHP。我的页面上加载了一些项目,当我单击它时,我想显示更多信息。这是我的连接数据库代码:
<?php
$servername = "localhost:3306";
$username = "root";
$password = "root";
$dbname = "webshop";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM items";
$result = $conn->query($sql);
$conn->close();
?>
数据库中的表是:
Id, name, prijs, cat
连接工作正常,我可以使用以下代码检索项目:
if($selected_val == "Vrouwen"){
echo'<section class="products">';
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if ($row['cat'] == 'vrouwen') {
$catvrouwen = $row['cat'];
echo "<div class='product-card'><button id='myBtn'><div class='product-image'><a href='index.php?id=" . $row['id'] . " '><img src='image/1.jpg'></a></div><h5>" . $row['name'] . '</h5><h6>' . '€' . $row['prijs'] . "</h6></button></div>";
}
$id = $row['id'];
$img = $row['img'];
$name = $row['name'];
$prijs = $row['prijs'];
echo $_POST['name'];
}
$result->close();
}
echo'</section>';
}
}
此代码运行良好,当我单击该项目时,我想查看有关它的更多信息。这是我写的代码:
if ($id = $_GET['id']) {
echo $id;
echo '<div class="info2"><div class="info-view">';
echo "<button id='myBtn'><div class='product-image'><a href=''><img src='image/1.jpg'></a></div><h5>";
echo $name;
echo "</h5><h6>€";
echo $prijs;
echo "</h6></button></div>";
echo '</div></div>';
}
我只能看到$id但$prijs和$name没有显示。有人可以帮我展示这个属性吗?亲切的问候
解决方案
我希望您在新页面上有此代码。做这个
if (isset($_GET['id'])) {
$stmt = $conn->prepare("SELECT * FROM items WHERE id = ?");
$stmt->bind_param("i", $_GET['id']);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
if (isset($row['id'])) {
echo $row['id'];
echo '<div class="info2"><div class="info-view">';
echo "<button id='myBtn'><div class='product-image'><a href=''><img src='image/1.jpg'></a></div><h5>";
echo $row['name'];
echo "</h5><h6>€";
echo $row['prijs'];
echo "</h6></button></div>";
echo '</div></div>';
}
}
推荐阅读
- python - 如何使用 pandas Styler 函数按列值为 html 表着色
- python-3.x - 快速清除qtablewidget内容的方法
- ssas - 如何将 where 子句编码到计算成员中?
- python-3.x - 在 python 3 中使用 'with' 和 'next()'
- angular - Angular 6 - 登录后更改页眉/页脚
- ghost-blog - Ghost 中的内部标签解析
- c# - Unity 3D & C#:将小数添加到浮点数,四舍五入
- servicestack - ServiceStack 自托管应用程序和 NotifyIcon
- swift4 - 尤里卡内联选择器行选择第一行
- r - 没有适用于“data.frame”类对象的“预测”方法