按下搜索按钮时循环表格

时间:2014-07-02 09:13:33

标签: php

在显示这些代码时,我的表循环有问题

<html>
<?php
$Candidate =$_POST ['candidate']; 
$link = mysqli_connect('localhost', 'root', '', 'test') or die(mysqli_connect_error());
$query = "SELECT * FROM `table 1` WHERE `fullname` LIKE '$Candidate%'";
$result = mysqli_query($link, $query) or die(mysqli_error($link));

mysqli_close($link);
$row=mysqli_fetch_assoc($result);


while ($row = mysqli_fetch_array($result))
{
 echo <table>
    echo "Name Of Candidate:". @$row['fullname'];
    echo "<br>";
    echo "comments:".@$row['comments'];
}

?>

最初我希望搜索结果以表格形式显示任何帮助吗?

2 个答案:

答案 0 :(得分:0)

您可以尝试以下

echo "<table>";
while ($row = mysqli_fetch_array($result))
{
 echo "<TR><TD>Name Of Candidate:" . $row['fullname'] . "</td>";
 echo "<TD>comments:" . $row['comments'] . "</TD></TR>";
}
echo "</table>";

答案 1 :(得分:0)

首先,您的代码容易受到MySQL注入攻击。见SO post

谈论表的呈现,下面的代码应该没问题:

$table = "<table>\n";
$tableHead = <<<THEAD
<thead>\n
    <tr>\n
      <th>Name of candidate</th>\n
      <th>Comments</th>\n
    </tr>\n
</thead>\n
THEAD;

//Add table head
$table .= $tableHead;

while ($row = mysqli_fetch_array($result)) {
   //No need for @ before $row, since your table will have those columns?
   $tableRow = <<<TABLEROW
   <tr>\n
     <td>{$row['fullname']}</td>\n
     <td>{$row['comments']}</td>\n
   </tr>\n
TABLEROW;
   $table .= $tableRow;
}

//Close the table
$table .= "</table>\n";

//Print the table
echo $table;
相关问题