Hello everybody,
I want to have search option and the search result in one page, so I write the following code, but I dont know why after search there is no results displayed:

  • The file name is SearchInSide.php
<HTML> 
<BODY> 
<FORM Action="SearchInSide.php" Method="POST"> 
<div align="center">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td bordercolor="#000000">
<p align="center">
<select name="metode" size="1">
<option value="name">Name</option>
<option value="telephone">Telephone</option>
<option value="birthday">Birthday</option>
</select> <input type="text" name="search" size="25"> &nbsp;<br>
Search database: <input type="submit" value="Go!!" name="Go"></p>
</td>
</tr>
</table>
</div>
</form>


<center>
<table border="1" cellpadding="5" cellspacing="0" bordercolor="#000000">
<tr>
<td width="60"><b>ID</b></td>
<td width="100"><b>Name</b></td>
<td width="70"><b>Telephone</b></td>
<td width="150"><b>Birthday</b></td>
</tr>

<?php
$hostname = "localhost"; // The Thinkhost DB server. 
$username = "root"; // The username you created for this database. 
$password = ""; // The password you created for the username. 
$usertable = "details"; // The name of the table you made. 
$dbName = "example"; // This is the name of the database you made. 

MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
@mysql_select_db( "$dbName") or die( "Unable to select database"); 
?> 
<?php

//error message (not found message)begins 
$XX = "No Record Found, to search again please try again"; 
//query details table begins

$query = mysql_query("SELECT * FROM details WHERE '$_POST[metode]' LIKE '$_POST[search]'% LIMIT 0, 50"); 
while ($row = @mysql_fetch_array($query)) 
{ 
$variable1=$row["id"];
$variable2=$row["name"]; 
$variable3=$row["telephone"]; 
$variable4=$row["birthday"];
//table layout for results 

print ("<tr>");
print ("<td>$variable1</td>"); 
print ("<td>$variable2</td>"); 
print ("<td>$variable3</td>"); 
print ("<td>$variable4</td>"); 
print ("</tr>"); 
}
//below this is the function for no record!!
if (!$variable1)
{ 
print ("$XX");
} 
//end 
?>
</table>
</center>
</BODY> 
</HTML>

i have re written your code(hope this helps..)

<HTML>
<BODY>
<FORM Action="" Method="POST">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td bordercolor="#000000">
<p align="center">
<select name="metode" size="1">
<option value="name">Name</option>
<option value="telephone">Telephone</option>
<option value="birthday">Birthday</option>
</select> <input type="text" name="search" size="25"> &nbsp;<br>
Search database: <input type="submit" value="Go!!" name="Go"></p>
</td>
</tr>
</table>
</div>
</form>


<center>
<table border="1" cellpadding="5" cellspacing="0" bordercolor="#000000">
<tr>
<td width="60"><b>ID</b></td>
<td width="100"><b>Name</b></td>
<td width="70"><b>Telephone</b></td>
<td width="150"><b>Birthday</b></td>
</tr>

<?php
if ($_POST)//user clicks go..
{
$hostname = "localhost"; // The Thinkhost DB server.
$username = "root"; // The username you created for this database.
$password = ""; // The password you created for the username.
$usertable = "details"; // The name of the table you made.
$dbName = "example"; // This is the name of the database you made.

MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
@mysql_select_db( "$dbName") or die( "Unable to select database");
?>
<?php

//error message (not found message)begins
$XX = "No Record Found, to search again please try again";
//query details table begins

$query = mysql_query("SELECT * FROM details WHERE '$_POST[metode]' LIKE '$_POST[search]'% LIMIT 0, 50");
while ($row = @mysql_fetch_array($query))
{
$variable1=$row["id"];
$variable2=$row["name"];
$variable3=$row["telephone"];
$variable4=$row["birthday"];
//table layout for results

print ("<tr>");
print ("<td>$variable1</td>");
print ("<td>$variable2</td>");
print ("<td>$variable3</td>");
print ("<td>$variable4</td>");
print ("</tr>");
}
//below this is the function for no record!!
if (!$variable1)
{
print ("$XX");
}
//end
}
?>
</table>
</center>
</BODY>
</HTML>

thanx for replying,
But this seems the same as my code, and actually it is not working too...

the reason why the query did not execute bcoz it does not read the statement in your query
try this query $query = mysql_query("SELECT * FROM topics WHERE '" .$_POST. "' LIKE '%".$_POST."%' LIMIT 0, 50")
or die("error");
and for easy tracking of error always put die

Thanks very much...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.