0

I have problems with looping of my table when displaying here are the codes

<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'];
}

?>

initially i want the search results to be displayed in a table format any help?

Mark Miller
  • 7,442
  • 2
  • 16
  • 22
  • You are missing some important parts of the table markup - and tags for instance. And you should be cautious with your LIKE-query, it's open for SQL injection. – cjs1978 Jul 02 '14 at 09:17
  • It is not good to loop table on search results. You should return tr of records – TBI Jul 02 '14 at 09:17
  • `` should be outside of `while` loop. and you are missing `` and `
    ` tags.
    – Prateek Jul 02 '14 at 09:18

2 Answers2

0

You can try following

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>";
Nebojsa Susic
  • 1,220
  • 1
  • 9
  • 12
0

First of all your code is vulnerable to MySQL injection attack. See this SO post

Talking about rendering of the table, the following code should do just fine:

$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;
Community
  • 1
  • 1
N3m1s
  • 139
  • 1
  • 3
  • thanks but my code is only catering to personal use not for public usage only a few people will be using the programme – user3796949 Jul 02 '14 at 09:37