我想要一个从数据库获取值的SQL Select语句,如果有任何结果,则在下拉菜单中显示一个Form和结果。
目前,它获取值,但表单与结果的次数相呼应。例如,如果查询有 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>';
}}
?>
www说
慕婉清6462132