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

cl25kdpy  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(314)

我第一次使用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没有显示。有人能帮我展示一下这个属性吗?谨致问候

rkttyhzu

rkttyhzu1#

我希望你在新的一页上有这个代码。做这个

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

相关问题