Creating a search form to search entries in a MQSQL DB
Posted: Mon Mar 02, 2009 2:51 pm
Hi Guys I am php novice but was wondering could anyone help me with a problem I am having.I am trying to create a search form on a HTML page what will search a Mysql DB and return what I have entered into the search bar along with its relevant information.I have created the follwing HTML page
I have then created the following search.php file
When I run this and submit a term into the search bar It returns what I have asked it to echo IE
id: '.$row['id']; echo '
message: '.$row['message']; echo '
'; } ?>
I have created HTML/php page all as one as follows
While this does what I want it to do IE search a MySQL db and return what I have searched along with other information because it is one page each of the entries into the field are displayed when I run the page in the browser initially and then disappear when I enter a term into the search bar (except the info I have actually searched for).Could anybody help me create 2 separate pages to do this function so that nothing is displayed initially apart from my search bar?
Code: Select all
<html>
<head>
<title>Search the Database</title>
</head>
<body>
<form action="search.php" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
Code: Select all
<?php
mysql_connect ("localhost", "root","password") or die (mysql_error());
mysql_select_db ("heskdb");
$term = $_POST['term'];
$sql = mysql_query("select * from hesk_std_replies where title like '%$term%'");
while ($row = mysql_fetch_array($sql)){
echo '<br/> id: '.$row['id'];
echo '<br/> message: '.$row['message'];
echo '<br/><br/>';
}
?>id: '.$row['id']; echo '
message: '.$row['message']; echo '
'; } ?>
I have created HTML/php page all as one as follows
Code: Select all
<html>
<head>
<title>Search the Database</title>
</head>
<body>
<form action="search2.php" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
<?php
mysql_connect ("localhost", "root","password") or die (mysql_error());
mysql_select_db ("heskdb");
$term = $_POST['term'];
$sql = mysql_query("select * from hesk_std_replies where title like '%$term%'");
while ($row = mysql_fetch_array($sql)){
echo '<br/> id: '.$row['id'];
echo '<br/> message: '.$row['message'];
echo '<br/><br/>';
}
?>