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条)

报告相同问题?

悬赏问题

  • ¥200 如何使用postGis实现最短领规划?
  • ¥15 pyinstaller打包错误
  • ¥20 cesm的气溶胶排放文件
  • ¥15 逐月累计,月份不连续,补齐月份
  • ¥15 应用简单的Python代码完成一个学生成绩管理系统
  • ¥15 用matlab求微分方程初值问题
  • ¥15 vscode下编写第三方库opencv与pcl代码时没有代码提示
  • ¥15 能够跑通不报错,如何解决?(标签-matlab)
  • ¥15 MOS在RDS较大,频率高时开关波形异常
  • ¥15 SCENIC分析报错求解答