===>if(array_key_exists('sub5',$_POST)) this line runs before i clicked on the submit while(true) before this line can't work then how can i solve the time problem? here is the whole code: <?php session_start(); $connection=Mysql_connect('localhost','admin','123'); if(array_key_exists('sub3',$_POST)) { if(!$connection) { echo 'connection is invalid'; } else { Mysql_select_db('drugnet',$connection); $query="select * from patient"; $result=mysql_query($query); $num=Mysql_num_rows($result); $num1=Mysql_num_fields($result); } if($num>0) { echo "<table border=2>"; for($i=0;$i<$num;$i++) { $row=mysql_fetch_row($result); echo "<tr>"; for($j=0;$j<$num1;$j++) { echo"<td>$row[$j]</td>"; } echo"<td><input type='submit' name='sub5' value='patient file$row[0]'/></td>"; echo"</tr>"; } echo"</table>"; } } if(array_key_exists('sub5',$_POST)) { $_SESSION[s1]=$row[0]; header("Location: patient.php?"); } ?> <html> <head> <title>Doctor</title> <form method="post"> <input type="submit" name="sub3" value="patient list"/> <input type="submit" name="sub4" value="search patient"/> </form> </head> </html>