gpt4 book ai didi

php - 单击 PHP 中的项目时如何获得更多信息?

转载 作者:可可西里 更新时间:2023-11-01 17:18:06 26 4
gpt4 key购买 nike

我是第一次使用 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>';
}
}

关于php - 单击 PHP 中的项目时如何获得更多信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53111494/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com