猿问

查询后PHP Echo重复

我想要一个 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>';


             }}

    ?>


慕沐林林
浏览 95回答 2
2回答

ABOUTYOU

创建一个变量$options并在返回数据时添加<option>到变量中。不要将所有html代码放在while.if ($result->num_rows > 0) {&nbsp; &nbsp;//Declare $options&nbsp; &nbsp;$options = '';&nbsp; &nbsp;while ($row = $result->fetch_assoc()) {&nbsp; &nbsp; &nbsp; //Adding <option> to the var $options&nbsp; &nbsp; &nbsp; $options .= '<option name="website_name">'. $row["website_name"]. '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </option>';&nbsp; &nbsp; }&nbsp; &nbsp; //HTML once, first part&nbsp; &nbsp;$html = '<h2>Download a website</h2>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <form action="downloads.php" method="get">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <select id="website_name" name="website_name">';&nbsp; &nbsp;//Adding <option> to the <select>&nbsp; &nbsp;$html .= $options;&nbsp; &nbsp;//HTML once, second part&nbsp; &nbsp;$html .= '</select>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" value="Download">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <hr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h2>Upload to a website</h2>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <form action="upload.php" method="post" enctype="multipart/form-data">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>Select file to upload:</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="file" name="zip_file" id="fileToUpload">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>Select a website to upload to:</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <select id="website_upload_name" name="website_upload_name">'&nbsp; &nbsp; //Adding <option> to the second <select>&nbsp; &nbsp; $html .= $options;&nbsp; &nbsp; //HTML once, third part&nbsp; &nbsp; $html .= '</select>&nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; <input type="submit" value="Upload" name="submit" style="position:relative; left: -1px;">&nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; <hr>';&nbsp; //Printing&nbsp; echo $html;&nbsp; }

杨__羊羊

它回显的次数与找到的结果一样多的原因是因为您已将回显语句放在 while 构造中。如果您希望在 if 语句的条件得到验证时仅显示一次回显,则将回显移到 while 之外,并将选项的 html 代码放在您将在 while 内构建的变量中。在这里,我想这两个组合都需要具有与原始代码相同的选项:if ($result->num_rows > 0) {$options = '';&nbsp; while ($row = $result->fetch_assoc()) {&nbsp; &nbsp; $options .= '<option name="website_name">'. $row["website_name"]. '</option>';&nbsp; &nbsp;&nbsp;&nbsp; }&nbsp; echo '<h2>Download a website</h2>&nbsp; &nbsp; &nbsp; &nbsp; <form action="downloads.php" method="get">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <select id="website_name" name="website_name">' . $options . '</select>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" value="Download">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</form>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<hr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<h2>Upload to a website</h2>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<form action="upload.php" method="post" enctype="multipart/form-data">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<p>Select file to upload:</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<input type="file" name="zip_file" id="fileToUpload">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<p>Select a website to upload to:</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<select id="website_upload_name" name="website_upload_name">' . $options . '</select>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<input type="submit" value="Upload" name="submit" style="position:relative; left: -1px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</form>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<hr>';}
随时随地看视频慕课网APP
我要回答