为了账号安全,请及时绑定邮箱和手机立即绑定

PHP 回显在查询后重复

PHP 回显在查询后重复

PHP
繁星coding 2022-08-05 15:33:23
我想要一个从数据库获取值的SQL Select语句,如果有任何结果,则在下拉菜单中显示一个Form和结果。目前,它获取值,但表单与结果的次数相呼应。例如,如果查询有 1 个结果,它将回显表单一次。2.结果,回声形式两次,依此类推。我希望表单显示一次,但下拉列表包含所有结果。如果有人能给我指出正确的方向,那就太好了。谢谢包含 2 个来自数据库的结果的图像<?php            include("conndetails.php");                 $conn = new mysqli($servername, $username, $password, $dbname);            $sql    = "SELECT website_name FROM user_websites WHERE username='$_SESSION[user]'"; //Selects all the websites for the user that is logged in.             $result = $conn->query($sql);              if ($result->num_rows > 0) {                  while ($row = $result->fetch_assoc()) {    echo '<h2>Download a website</h2>          <form action="downloads.php" method="get">          <select id="website_name" name="website_name">          <option name="website_name">'. $row["website_name"]. '          </option>          </select>          <input type="submit" value="Download">          </form>          <br>          <hr>          <h2>Upload to a website</h2>          <form action="upload.php" method="post" enctype="multipart/form-data">           <p>Select file to upload:</p>          <input type="file" name="zip_file" id="fileToUpload">          <p>Select a website to upload to:</p>          <select id="website_upload_name" name="website_upload_name">          <option name="website_upload_name">'. $row["website_name"]. '          </option>          </select>          <br>          <br>          <input type="submit" value="Upload" name="submit" style="position:relative; left: -1px;">          </form>          <br>          <hr>';             }}    ?>
查看完整描述

2 回答

?
www说

TA贡献1775条经验 获得超8个赞

创建一个变量,并在返回数据时添加到该变量中。


不要将所有代码都放在 .$options<option>htmlwhile


if ($result->num_rows > 0) {


   //Declare $options

   $options = '';



   while ($row = $result->fetch_assoc()) {

      //Adding <option> to the var $options

      $options .= '<option name="website_name">'. $row["website_name"]. '

          </option>';

    }




    //HTML once, first part

   $html = '<h2>Download a website</h2>

          <form action="downloads.php" method="get">

          <select id="website_name" name="website_name">';


   //Adding <option> to the <select>

   $html .= $options;


   //HTML once, second part

   $html .= '</select>

          <input type="submit" value="Download">

          </form>

          <br>

          <hr>


          <h2>Upload to a website</h2>

          <form action="upload.php" method="post" enctype="multipart/form-data"> 

          <p>Select file to upload:</p>

          <input type="file" name="zip_file" id="fileToUpload">

          <p>Select a website to upload to:</p>

          <select id="website_upload_name" name="website_upload_name">'


    //Adding <option> to the second <select>

    $html .= $options;


    //HTML once, third part

    $html .= '</select>

      <br>

      <br>

      <input type="submit" value="Upload" name="submit" style="position:relative; left: -1px;">

      </form>

      <br>

      <hr>';



  //Printing

  echo $html;



  }


查看完整回答
反对 回复 2022-08-05
?
慕婉清6462132

TA贡献1804条经验 获得超2个赞

它回显的时间与找到的结果一样多的原因是,您已经将回显语句放在 while 构造中。如果您希望回显仅在验证 if 语句的条件时显示一次,请移动回显到 while 外部,并将选项的 html 代码放在变量中,您将在一段时间内构建这些变量。在这里,我想这两个组合都需要具有与原始代码相同的选项:


if ($result->num_rows > 0) {


$options = '';


  while ($row = $result->fetch_assoc()) {


    $options .= '<option name="website_name">'. $row["website_name"]. '</option>';    


  }


  echo '<h2>Download a website</h2>

        <form action="downloads.php" method="get">

          <select id="website_name" name="website_name">' . $options . '</select>

          <input type="submit" value="Download">

         </form>

         <br>

         <hr>


         <h2>Upload to a website</h2>

         <form action="upload.php" method="post" enctype="multipart/form-data"> 

           <p>Select file to upload:</p>

           <input type="file" name="zip_file" id="fileToUpload">

           <p>Select a website to upload to:</p>

           <select id="website_upload_name" name="website_upload_name">' . $options . '</select>

           <br>

           <br>

           <input type="submit" value="Upload" name="submit" style="position:relative; left: -1px;">

         </form>

         <br>

         <hr>';


}


查看完整回答
反对 回复 2022-08-05
  • 2 回答
  • 0 关注
  • 93 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信