Tuesday, June 5, 2012

AND Search php

AND Search php

Previous lessen we were created search files with single search field.Now we will create files with 2 search fields using the connection word AND.search HTML designed for purpose is bellow
 
Select from First name and Last Name
First Name:
Last Name:
Codes for above HTML are

<html>
<head>
<title>Search</title>
</head>
<body>
<div id="formContent" align="center">
        <form action="search1.php" method="post" enctype="multipart/form-data">

        <fieldset style="width: 100px; height: 200px;">
            <legend>Select from First name and Last Name</legend>

            <table bgcolor=#b7bb72>
                <tr bgcolor=#ffffff><td>First Name: </td><td><input type="text" name="term1" value=""></td></tr>
                <tr bgcolor=#ffffff><td>Last Name: </td><td><input type="text" name="term2" value=""></td></tr>  
                                                                 <tr bgcolor=#ffffff ><td><input type="submit" id="submit" name="submit" value="Search"></td></tr>
</table>
</fieldset>
        </form>
    </div>

</div>
</table>
</body>
</html>

search1 php is bellow which search from your "person" table with First name and Last Name both

<html>
<head>
<title><?php print("("); print date("l"); print(", "); print date("d/m/y"); print(")");?></title>
</head>
<?php
$host="localhost";
$username="";
$password="";
$db_name="my_database";
$tbl_name="person";
$term1=$_POST['term1'];
$term2=$_POST['term2'];
mysql_connect("$host","root","$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name WHERE FirstName LIKE '$term1%' AND LastName LIKE '$term2%'";
$result=mysql_query($sql);

?>
<table align="center" bgcolor="#b3dfef">

<tr>
<td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>First Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Last Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Birth Day</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['FirstName']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['LastName']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['birth_day']; ?></td>

<?php
}
mysql_close();
?>
</table>
</body>
</html>

No comments:

Post a Comment