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

查询后PHP Echo重复

查询后PHP Echo重复

PHP
慕沐林林 2022-07-22 18:55:50
我想要一个 SQL Select 语句,它从数据库中获取值,如果有任何结果,那么在下拉菜单中显示一个表单和结果。目前,它获取值,但表单回显有结果的次数。例如,如果查询有 1 个结果,它会回显表单一次。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 回答

?
ABOUTYOU

TA贡献1812条经验 获得超5个赞

创建一个变量$options并在返回数据时添加<option>到变量中。


不要将所有html代码放在while.


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-07-22
?
杨__羊羊

TA贡献1943条经验 获得超7个赞

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


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-07-22
  • 2 回答
  • 0 关注
  • 94 浏览

添加回答

举报

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