2

I've made a script for a search mysql table, and instead of one table with more rows, he return me on results page one table for each person.

This is my results code by id from the search script. and returns table for each person

    $id = $_GET['id']; 
    $sql = "select * from wp_certificari WHERE id = '{$id}'";
    $rst = mysql_query($sql); 
    while($a_row = mysql_fetch_assoc($rst))  { 
    echo "<center>\n";
    echo "<b>Detalii Certificat:</b>";
    echo "<table border='1'>"; 
    echo"<thead>"; 
    echo "<th>Denumire certificare</th>"; 
    echo "<th>Serie si numar certificare</th>"; 
    echo "<th>Data certificarii</th>"; 
    echo "<th>Valabilitate certificare</th>"; 
    echo "<th>Sector Financiar</th></tr>"; 
    echo"</thead>";
    echo "<td class='lalign'>{$a_row['nume']}</td>" ;  
    echo "<td class='lalign'>{$a_row['serie_numar']}</td>" ; 
    echo "<td class='lalign'>{$a_row['data']}</td>" ; 
    echo "<td class='lalign'>{$a_row['valabilitate']}</td>" ; 
    echo "<td class='lalign'>{$a_row['sector_financiar']}</td></tr>" ;      
    echo"</table>";
    echo "</center>\n";}

Return image

table

Saty
  • 22,443
  • 7
  • 33
  • 51
  • Well, you output the markup for one table in each iteration of the loop. So you _programmed_ to get one table for each row in the query result. Put the table markup outside the `while` loop and keep only the table _rows_ inside the loop. So that you output one html table row for each row in the queries result set. – arkascha Sep 16 '15 at 08:20
  • A general note: you have no error handling whatsoever in your code. you blindly trust that the query will succeed. If it fails (whyever), then you will see a broken page. you want to add error detection and handling to your code. – arkascha Sep 16 '15 at 08:23

2 Answers2

2

You need to write your table and table head html outside your while loop Add mysql_num_rows to check empty result and add mysql_real_escape_string to Escapes special characters in a string for use in an SQL statement

$id = $_GET['id']; 
    $id=mysql_real_escape_string($id);
    $sql = "select * from wp_certificari WHERE id = '{$id}'";
    $rst = mysql_query($sql); 
    $row=mysql_num_rows($rst);
    if($row>0){
    //outside while loop
    echo "<center>\n";
    echo "<b>Detalii Certificat:</b>";
    echo "<table border='1'>"; 
    echo"<thead>"; 
    echo "<th>Denumire certificare</th>"; 
    echo "<th>Serie si numar certificare</th>"; 
    echo "<th>Data certificarii</th>"; 
    echo "<th>Valabilitate certificare</th>"; 
    echo "<th>Sector Financiar</th>"; 
    echo"</thead>";
    while($a_row = mysql_fetch_assoc($rst))  { 

    echo "<tr><td class='lalign'>{$a_row['nume']}</td>" ;  
    echo "<td class='lalign'>{$a_row['serie_numar']}</td>" ; 
    echo "<td class='lalign'>{$a_row['data']}</td>" ; 
    echo "<td class='lalign'>{$a_row['valabilitate']}</td>" ; 
    echo "<td class='lalign'>{$a_row['sector_financiar']}</td></tr>" ;      
   }
    //outside while loop
    echo"</table>";
    echo "</center>\n";
   }

Note:- Don't use mysql because it is deprecated instead use mysqli or PDO

To prevent sql injection check this link How can I prevent SQL-injection in PHP?

Community
  • 1
  • 1
Saty
  • 22,443
  • 7
  • 33
  • 51
1

You have to get table part outside from while loop.

$id = $_GET['id']; 
$sql = "select * from wp_certificari WHERE id = '{$id}'";
$rst = mysql_query($sql); 

echo "<center>\n";
echo "<b>Detalii Certificat:</b>";
echo "<table border='1'>"; 
echo "<thead>";
echo "<tr>"; 
echo "<th>Denumire certificare</th>"; 
echo "<th>Serie si numar certificare</th>"; 
echo "<th>Data certificarii</th>"; 
echo "<th>Valabilitate certificare</th>"; 
echo "<th>Sector Financiar</th></tr>"; 
echo "</thead><tbody>";

while($a_row = mysql_fetch_assoc($rst))  { 
     echo "<tr>";
     echo "<td class='lalign'>{$a_row['nume']}</td>" ;  
     echo "<td class='lalign'>{$a_row['serie_numar']}</td>" ; 
     echo "<td class='lalign'>{$a_row['data']}</td>" ; 
     echo "<td class='lalign'>{$a_row['valabilitate']}</td>" ; 
     echo "<td class='lalign'>{$a_row['sector_financiar']}</td></tr>";      
}

echo"</tbody></table>";
echo "</center>\n";