dongxian3574 2017-06-04 17:41
浏览 72
已采纳

如何将从数据库检索的数据插入到预先设置的HTML标记中?

I want to display the data I retrieve from a database table in styled HTML. Please look at the code below, the commented out line works and I would like to fill in the data in the <section class="profile"> below the comment. Thanks in advance

I know my code is vulnerable, but I want to get it to work before I worry about security.

<body>
<div id="wrapper" class="menu">
    <?php require('inc-nav.php') ?>

    <section class="content">

        <?php
        require('connect.php');

        $conn = mysqli_connect($servername, $username, $password, $dbname);
        $sql = "SELECT name, breed, age FROM dogs";
        $result = mysqli_query($conn, $sql);

        if (mysqli_num_rows($result) > 0) {
            // output data of each row
            while($dogs = mysqli_fetch_assoc($result)) {
//          echo 'Name: ' . $dogs['name'] . '<br>' . 'Breed: ' . $dogs['breed'] . '<br>' . 'Age: ' . $dogs['age'] . '<br><hr>';
                echo '
                <section class="profile">
                    <figure class="profile-pic"><img src="images/1.jpg" alt=""></figure>
                    <h1 class="name">Name</h1>
                    <h2 class="description1">Breed | gender | age</h2>
                    <p class="description2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Architecto corporis dolores dolorum eaque eum in nemo quos totam ullam. Autem?</p>
                    <button class="cta-profile"><a href="#">Read more</a></button>
                </section>
                ';
            }
        } else {
            echo "0 results";
        }

        mysqli_close($conn);
        ?>

    </section>

    <?php require('inc-footer.php');?>
</div>
  • 写回答

2条回答 默认 最新

  • dongyao2129 2017-06-04 17:51
    关注

    You want like this

    <body>
    <div id="wrapper" class="menu">
        <?php require('inc-nav.php') ?>
    
        <section class="content">
    
            <?php
            require('connect.php');
    
            $conn = mysqli_connect($servername, $username, $password, $dbname);
            $sql = "SELECT name, breed, age FROM dogs";
            $result = mysqli_query($conn, $sql);
    
            if (mysqli_num_rows($result) > 0) {
                // output data of each row
                while($dogs = mysqli_fetch_assoc($result)) {
    
                 ?>
                    <section class="profile">
                        <figure class="profile-pic"><img src="images/1.jpg" alt=""></figure>
                        <h1 class="name"><?php echo 'Name: ' .$dogs['name']; ?></h1>
                        <h2 class="description1"> <?php echo 'Breed: ' . $dogs['breed']; ?>  | gender | <?phpecho  'Age: ' . $dogs['age']; ?></h2>
                        <p class="description2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Architecto corporis dolores dolorum eaque eum in nemo quos totam ullam. Autem?</p>
                        <button class="cta-profile"><a href="#">Read more</a></button>
                    </section>
                    <?php
                }
            } else {
                echo "0 results";
            }
    
            mysqli_close($conn);
            ?>
    
        </section>
    
        <?php require('inc-footer.php');?>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd