gpt4 book ai didi

php - 为什么我的表格中只显示一个结果?

转载 作者:行者123 更新时间:2023-11-29 12:15:23 24 4
gpt4 key购买 nike

我正在使用 php my mysqli,当用户从数据库中选择房屋 id 时,并使用选择查询在表中显示结果。由于原因,即使有两行使用相同的数字,它也只显示一行,知道如何解决这个问题。

<form action="back.php" method="post"> 

<p>Chose a house ID</p>

<select name="HouseID">


<?php

include "connect.php";

$query = "SELECT distinct `HouseID` from purchase";
$result = mysqli_query($con, $query) or die("Invalid query");
while($rows = mysqli_fetch_array($result))

{

echo "<option value=\"" . $rows[0] . "\">" . $rows[0] ."</option>";
}

echo "</select>";


mysqli_close($con);
?>

<p>Choose the ORDER BY attribute</p>

<select name ="list">

<option value="PurchaseID">PurchaseID</option>
<option value="HouseID">HouseID</option>
<option value="DatePurchased">DatePurchased</option>
<option value="DatePurchased">DatePurchased</option>
<option value="AskingFor">AskingFor</option>
<option value="SoldFor">SoldFor</option>
<option value="AgentID">AgentID</option>
<option value="CustomerID">CustomerID</option>
</select>

<p>Choose ASC or DESC attribute order</p>

ASC<input type="radio" name="order" value="ASC" checked>

DESC<input type="radio" name="order" value="DESC" checked>

<br>
<br>
<input type="submit" value="Submit Value">
</form></body></html>

<?php

$HouseID = $_POST["HouseID"];
$list = $_POST["list"];
$order = $_POST["order"];

include "connect.php";

$query = "SELECT `HouseID`,`AskingPrice`,`SoldFor`,`DatePurchased` FROM purchase where `HouseID` = $HouseID ORDER BY ";
$result = mysqli_query($con, $query) or die($mysqli->connect_error);

echo "<table border='1'><tr><th>House ID</th><th>Asking Price</th><th>Sold For</th><th>Date Purchased</th></tr>";
$row = mysqli_fetch_array($result);


echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td><td>" . $row[2] . "</td><td>" . $row[3] . "</td></tr>";
echo "</table>";
mysqli_free_result($result);
mysqli_close($conn);

?>

最佳答案

您需要循环遍历数组。

否则,它将仅显示最后一个元素。

echo "<table border='1'><tr><th>House ID</th><th>Asking Price</th><th>Sold For</th><th>Date Purchased</th></tr>";  
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td><td>" . $row[2] . "</td><td>" . $row[3] . "</td></tr>";
}
echo "</table>";
mysqli_free_result($result);
mysqli_close($conn);

关于php - 为什么我的表格中只显示一个结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29894895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com