0

I want to display a row when a user enter his id and after submit. My code is not working correctly. Please rectify this.

search-form.php is looking like this.

</html><body>
 <form method="GET" action="search.php">
  Keyfield <input type="text" name="search"> <br><br>
  <input type="submit"  value="submit">
</form></body>
</html>

and

search.php looking like this.

<?php

$connection =     mysql_connect('localhost','user','pass')     or die ("Couldn't connect to server."); 
$db = mysql_select_db('db', $connection) or die ("Couldn't select     database."); 

$search=$_GET['search'];

$fetch = 'SELECT * FROM `table` WHERE `ID` = "'.$search.'"'; 
  echo "<table margin=auto width=999px border=1>";
        echo "<tr><td><b>ID</b></td><td><b>Name</b></td><td><b>Telephone</b></td><td>    <b>E-mail</b></td><td><b>Couttry Applying for</b></td><td><b>Visa-Category</b>    </td><td><b>Other Category</b></td><td><b>Passport No</b></td><td>    <b>Remarks</b></td></tr>";
    for($i=0;$i<$num;$i++)
    {
    $row=mysql_fetch_row($fetch);
    echo "<tr>";
    echo"<td>$row[0]</td>";
    echo"<td>$row[1]</td>";
    echo"<td>$row[2]</td>";
    echo"<td>$row[3]</td>";
    echo"<td>$row[4]</td>";
    echo"<td>$row[5]</td>";
    echo"<td>$row[6]</td>";
    echo"<td>$row[7]</td>";
    echo"<td>$row[8]</td>";
    echo"</tr>";
    }//for
    echo"</table>";
?>

Display when a user enter his id and submit. But this code doesn't display row with id. Rectify this. Thanks.

user2520162
  • 33
  • 1
  • 1
  • 4

2 Answers2

0

you are missing mysql_query statement.You should execute the query before fetching the result modify like $sql= 'SELECT * FROM table WHERE ID = "'.$search.'"'; $fetch = mysql_query($sql);

shanavascet
  • 589
  • 1
  • 4
  • 18
0

A few points:

  1. mysql_connect is obsolete, do not use it. Use PDO instead for example.
  2. $fetch = 'SELECT * FROMtableWHEREID= "'.$search.'"' leads to the most common and severe security flaw : SQL injection. Please read about this (google)
  3. Where do you "fetch" the result of your query ?

About point 3, assuming the fact that you will use PDO, please read http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html

PierreL
  • 460
  • 4
  • 7