从数据库收到的值未在php中正确显示

时间:2020-05-01 16:38:06

标签: php html

大家好,请帮帮我,我有这个php代码,可从数据库中获取数据并显示在网络上,如果您选中下面的 input隐藏标签 ,我给了它一个值,但是并不是所有的值都能正确显示。而是创建一个属性。

PHP代码

<?php
   $output = '';
    $sqlNO = "SELECT * FROM pnewoffer";

     $NOresult = mysqli_query($conn, $sqlNO);
     if(mysqli_num_rows($NOresult) > 0) {
         while($row = mysqli_fetch_array($NOresult)) {
         $output .= '<div class="offer-card">
          <form action="./bked/savedit.php" method="post">
            <a href="#" class="offer-card-inner">
            <div class="offer-img">
              <img src="./image/'.$row['offerPImage'].'" alt="New Offer Image" width="200">
            </div>
          <div class="offer-info">
               <h3 class="offer-title">'.$row['offerPName'].'</h3>
                  <div class="clearfix rating marT8 ">
                     <div class="rating-stars ">
                        <div class="grey-stars"></div>
                         <div class="filled-stars" style="width:60.0%"></div>
                      </div>
                   </div>
                   <h4 class="offer-product-price">N'.$row['offerPPrice'].' </h4>
          </div>
       </a>
                    <input type="hidden" name="newPPrice" value='.$row['offerPPrice'].'>
                    <input type="hidden" name="newPName" value='.$row['offerPName'].'>
                    <input type="hidden" name="newPImg" value='.$row['offerPImage'].'>

       <div class="offer-bt-btn">
          <div class="offer-btn">
             <button type="submit" name="saveForLater" class="favorite-offer-btn">
                <i class="far fa-heart"></i>
             </button>
              <a href="#" class="offer-product-info-btn">
               <i class="fa fa-info"></i>
               </a>
           </div>
       </div>

     </form>
   </div>';
   }
  }

  echo $output;

?>

结果

<input type="hidden" name="newPName" value="Blue" louis="" vuitton="" women="" bag="">
<input type="hidden" name="newPName" value="Samsung" galaxy="" s9="" 6gb="" ram,="" 32gb="" rom,="" 16mpbs="">

实际上应该是

<input type="hidden" name="newPName" value="Samsung Galaxy S9 6gb ram, 32gb rom, 16mpbs">
<input type="hidden" name="newPName" value="Blue Louis Vuitton Women Bag">

请,我该怎么办?

1 个答案:

答案 0 :(得分:-1)

如果任何值包含'个字符,则将结束value属性并开始一个新属性。使用htmlspecialchars()对其进行编码并防止这种情况。

<input type="hidden" name="newPPrice" value='.htmlspecialchars($row['offerPPrice'], ENT_QUOTES).'>
<input type="hidden" name="newPName" value='.htmlspecialchars($row['offerPName'], ENT_QUOTES).'>
<input type="hidden" name="newPImg" value='.htmlspecialchars($row['offerPImage'], ENT_QUOTES).'>