我是第一次使用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 。 有人可以帮助我显示此属性吗? 问候
答案 0 :(得分:0)
我希望您在新页面上有此代码。做
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>';
}
}