首页 > 解决方案 > 单击 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没有显示。有人可以帮我展示这个属性吗?亲切的问候

标签: phphtmlmysqlhttpphpmyadmin

解决方案


我希望您在新页面上有此代码。做这个

 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>';
    }
    }

推荐阅读