Tuesday, June 5, 2012

OR Search php

OR Search php

This tutorial search by First Name OR Last Name either one of them correct search will be completed.The files are not so different from AND search.
 
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="search2.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>

search2 php is bellow which search from your "person" table with First name orLast Name.

<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' OR 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