我无法将数据回显到HTML表格中。
它是这样的:
但它应该是:
这是代码。我做错了什么?
<?php
$query = $_POST['query'];
$min_length = 1;
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM norse5_proov
WHERE (`model` LIKE '%".$query."%') OR (`year` LIKE '%".$query."%')") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
echo "<table>";
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "<br>";
echo "</table>";
}
}
else{
echo "No results";
}
}
?>
答案 0 :(得分:2)
问题是你不断为每次迭代输出一个新表。
您的代码应如下所示:
<?php
$query = $_POST['query'];
$min_length = 1;
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM norse5_proov
WHERE (`model` LIKE '%".$query."%') OR (`year` LIKE '%".$query."%')") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
echo "<table>"; // Start the table
// Output the table headers
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
while($results = mysql_fetch_array($raw_results)) {
echo "<tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "<br>";
echo "</tr>";
}
echo "</table>"; // End the table
}
else{
echo "No results";
}
}
?>
答案 1 :(得分:0)
只需将echo "<table>";
和第一个tr
创建语句放在while循环之外,并在完成table
循环后关闭while
,看看我确定它会起作用
尝试
<?php
echo "<table><tr><td>Model name</td><td>Year</td></tr>";
while($results = mysql_fetch_array($raw_results))
{
echo "<tr><td>".$results['mudeli_nimetus']."</td><td>".$results['soetusaasta']."</td></tr>";
}
echo "</table>";
?>
答案 2 :(得分:0)
使用此代码,您必须先启动表,使用while循环迭代结果,然后关闭表。
echo "<table>";
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
while($results = mysql_fetch_array($raw_results)){
echo "<tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "</tr>";
}
echo "</table>";
答案 3 :(得分:0)
从while循环中删除表代码并放在外面。
echo "<table>";
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
while($results = mysql_fetch_array($raw_results)){
echo "<tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "</tr>";
}
echo "</table>";
答案 4 :(得分:0)
替换你的if块,使用以下代码,希望它可以帮助你
if(mysql_num_rows($ raw_results)&gt; 0) {
echo "<table>";
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
while($results = mysql_fetch_array($raw_results)){
echo "<tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "</tr>";
}echo "</table>";
}