Creating a search form to search entries in a MQSQL DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ciaran1987
Forum Newbie
Posts: 5
Joined: Mon Mar 02, 2009 2:48 pm

Creating a search form to search entries in a MQSQL DB

Post by Ciaran1987 »

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

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>
 
I have then created the following search.php file

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/>';
    }
  
?>
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

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/>';
    }
 
?>
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?
Last edited by Ciaran1987 on Mon Mar 02, 2009 3:55 pm, edited 1 time in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Creating a search form to search entries in a MQSQL DB

Post by Benjamin »

Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]
Post Reply