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

时间:2017-06-04 17:41:32

标签: php html database styling

我想在样式化的HTML中显示从数据库表中检索的数据。 请查看下面的代码,注释掉的行可以使用,我想填写评论下面<section class="profile">的数据。 提前致谢

我知道我的代码容易受到攻击,但我想在担心安全问题之前让它工作。

<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 个答案:

答案 0 :(得分:0)

p2

答案 1 :(得分:0)

你想要这样

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