2017-06-04 54 views
-1

我想顯示我從樣式HTML中的數據庫表中檢索到的數據。 請看下面的代碼,註釋掉的行可以工作,我想在註釋下方的<section class="profile">填寫數據。 在此先感謝如何將從數據庫檢索到的數據插入到預先設計好的HTML標籤中?

我知道我的代碼易受攻擊,但我希望在我擔心安全性之前讓它工作。

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

你必須有所有的代碼,你嘗試過什麼? –

+0

你的代碼容易受到什麼影響? – chris85

回答

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> 
0
echo ' 
       <section class="profile"> 
        <figure class="profile-pic"><img src="images/1.jpg" alt=""></figure> 
        <h1 class="name">'.$dogs["name"].'</h1> 
        <h2 class="description1">'.$dogs["breed"].' | '.$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> 
       '; 
相關問題