douyi4205 2018-11-02 01:20
浏览 49
已采纳

如何在PHP中单击某个项目时获取更多信息?

I working with PHP for the first time. I got some items loaded on my page and when I click on it I want to show more information. This is my connection database code:

<?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();


?>

The tables in the database are:

Id, name, prijs, cat

The connection is working fine and I can retrieve the items with the following code:

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

}

This code is working fine and when I click on the item I want to see more information about it. This is the code I wrote:

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

I can see only $id but $prijs and $name is not showing. Can someone help me to show this attributes? Kind regards

  • 写回答

1条回答 默认 最新

  • douzhe9075 2018-11-02 01:30
    关注

    I hope you are having this code on a new page. Do this

     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>';
        }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理