PHP code to search a record on button-click. 

search

 

soutp

here is PHP code (search.php) !

<form action="search.php" method="post">
<?php
$hid="style='visibility:hidden'"; 
$r1="";$r2="";$r3="";$nt="";
$codeerr="";
$code="";
$flag=0;
if(isset($_POST["submit"]))
{
    if(empty($_POST["t1"]))
	{
		$codeerr="Data Missing";
		$flag=0;
		}
		else if(is_numeric(trim($_POST["t1"]))==false)
		{
			$codeerr="Accept Numeric value only";
			$flag=0;
		}
		else
		{
			$code=$_POST["t1"];
			$flag=1;
			}
}

if($flag!=0)
{
	$server='localhost';
	$username='root';
	$password='';
	$cnt=0;
	try
	{
		$db=new PDO("mysql: host=$server; dbname=softaid",$username, $password);
		$result=$db->query('select *from student where rollno='.$code);
		$result->setFetchMode(PDO::FETCH_ASSOC);
		$row=$result->fetch();
		$cnt=count($row);
		if($cnt>1)
		{
						
			$r1=$row["rollno"]; 		
			
			$r2=$row["sname"]; 
			
			$r3=$row["scourse"];
			$hid="style='visibility:visible'"; 
		
		}
		
		else
		{
			$hid="style='visibility:visible'"; 
			$nt="<span style='color:red'>Data not found...!</span>";
		}
		}
			 
    
	catch(PDOException $e)
	{
		echo $e->getMessage();
	}
}
 
?>
<!-- Designing of Search Table -->
<table  style="margin:auto; background-color:#FFF; color:#C03;   ">
<tr>

</tr>
<tr>

</tr>
</table>


<div style="width:695px; padding:13px 0px; border-radius:30px 30px 0 0; background-color:#00A7CA; color:#FFF; margin:auto; font-size:20px;font-family:Tahoma, Geneva, sans-serif; text-align:center;">Search</div>
<table  style="width:695px; margin:auto; font-family:Tahoma, Geneva, sans-serif; color:#666; background-color:#FF9; text-align:center">
<tr>
<td >Enter Student Roll No.</td>
<td width="300px"><input type="text" name="t1" style="width:100%; height:25px" placeholder="<?php echo $codeerr ?>"  /></td>
<td >
<input type="submit" name="submit" value="search" style="padding:3px; width:100px; font-size:18px" />
 </td>
</tr>
</table>

<div style="width:695px; padding:20px 0px; border-radius:0px 0px 20px 20px; background-color:#00A7CA; color:#FFF; margin:auto; font-size:18px; text-align:center;"></div>

<br />

<div <?php echo $hid; ?>>
<div style="width:500px; padding:13px 0px; border-radius:30px 30px 0 0; background-color:#00A7CA; color:#FFF; margin:auto; font-size:20px;font-family:Tahoma, Geneva, sans-serif; text-align:center;">Searching Result</div>
<table  border="1" style="width:500px; margin:auto; font-family:Tahoma, Geneva, sans-serif; color:#666; background-color:#FF9; text-align:center">
<tr>
<th>Roll NO.</th><th>Name</th><th>Course</th>
</tr>
<tr><td colspan="3"><?php echo $nt; ?></td></tr>
<tr>
<td><?php echo $r1; ?></td><td><?php echo $r2; ?></td><td><?php echo $r3; ?></td>
</tr>
</table>

<div style="width:500px; padding:20px 0px; border-radius:0px 0px 20px 20px; background-color:#00A7CA; color:#FFF; margin:auto; font-size:18px; text-align:center;"></div>
</div>
</form>

Your Database here.

seard