• home
  • forum
  • my
  • kt
  • download
  • Creating a Form that will Search a MySQL Database

    Author: 2009-03-09 08:02:55 From:

    For our search to work we will have to create two files. One file will be the PHP script to search our form and the other will be an HTML page containing our form and passing the search variable to our PHP file.
    I will start by create an search.htm file. Below you will see the basic structure of our simple HTML page.

    view plaincopy to clipboardprint?
    1.   
    2. <html>  
    3.     <head>  
    4.         <title>Search the Database</title>  
    5.     </head>  
    6.   
    7.     <body>  
    8.   
    9.     </body>  
    10. </html>  

    Next we will add the form. Notice that the action=”search.php” and the method=”post”. This is basically telling the web server to send Post variables to the search.php page. Another important thing to note is the input box name field. This field, name=”term” , this will pass whatever is in the input box on as a post variable named “term”. We also add a submit button.

    view plaincopy to clipboardprint?
    1.   
    2. <html>  
    3.     <head>  
    4.         <title>Search the Database</title>  
    5.     </head>  
    6.   
    7.     <body>  
    8.   
    9.     <form action="search.php" method="post">  
    10.      Search: <input type="text" name="term" /><br />  
    11.     <input type="submit" name="submit" value="Submit" />  
    12.     </form>  
    13.   
    14.     </body>  
    15. </html>  

    Now on to search.php . The first thing I did was to simple echo the post variable to make sure the information is getting passed from the search form.

    view plaincopy to clipboardprint?
    1.   
    2. <?php   
    3.       echo $_POST['term'];   
    4. ?>  

    If you variable is not being displayed then something is wrong. If your search term is displayed then you can move on. We can delete the echo code from our php file now.
    First make a connection to your database

    view plaincopy to clipboardprint?
    1.   
    2. <?php   
    3.   
    4. mysql_connect ("localhost""testuser","password")  or die (mysql_error());   
    5.   
    6. ?>  

    Now select your test database

    view plaincopy to clipboardprint?
    1.   
    2. <?php   
    3.   
    4. mysql_connect ("localhost""testuser","password")  or die (mysql_error());   
    5. mysql_select_db ("test");   
    6.   
    7. ?>  

    Next we are going to store the post variable as $term and build our query. As you can see we are searching the first name field. We are searching for a first name like %$term%. The % character is a wild card for 0 or more charcters. So if we had ‘bob’ in our database and we entered the ‘bob’ in the search box it would return the results. With the % charcters around the term we could also search for ‘ob’ and it would return the results for bob, and any other name containing ‘ob’.

    view plaincopy to clipboardprint?
    1.   
    2. <?php   
    3. mysql_connect ("localhost""testuser","password")  or die (mysql_error());   
    4. mysql_select_db ("test");   
    5.   
    6. $term = $_POST['term'];   
    7.   
    8. $sql = mysql_query("select * from testtable where FName like '%$term%'");   
    9.   
    10. ?>  

    The next step is to execute the query and display the results.

    view plaincopy to clipboardprint?
    1.   
    2. <?php   
    3. mysql_connect ("localhost""testuser","password")  or die (mysql_error());   
    4. mysql_select_db ("test");   
    5.   
    6. $term = $_POST['term'];   
    7.   
    8. $sql = mysql_query("select * from testtable where FName like '%$term%'");   
    9.   
    10. while ($row = mysql_fetch_array($sql)){   
    11.     echo 'ID: '.$row['ID'];   
    12.     echo '<br/> First Name: '.$row['FName'];   
    13.     echo '<br/> Last Name: '.$row['LName'];   
    14.     echo '<br/> Phone: '.$row['Phone'];   
    15.     echo '<br/><br/>';   
    16.     }   
    17.   
    18. ?>  

    Now I will test it out. Since I know ‘Bob’ is in the database I will search for him.

    If all goes well you should have these results returned/

    So we can search for First names only. If you want to give the user the ability to enter either a first name or a last name change your query to this:

    view plaincopy to clipboardprint?
    1.   
    2. $sql = mysql_query("select * from testtable where FName like '%$term%' or LName like '%$term%' ");  

    To test that query I did a simple search for the letter ‘o’. Which should return several first names and last names that contain the letter.

    So there you have it a simple basic database search form. I hope this all makes sense, I am writing this at 4:00am and am a bit tired. As always feel free to ask questions.

    discuss this topic to forum

    relation tutorial

    No information

    Category

      Ad Management (6)
      Calendars (3)
      Chat Systems (8)
      Content Management (41)
      Cookies and Sessions (12)
      Counters (15)
      Database Related (34)
      Date and Time (15)
      Development (22)
      Discussion Boards (8)
      E Commerce (8)
      Email Systems (14)
      Error Handling (8)
      File Manipulation (36)
      Flash and PHP (6)
      Form Processing (20)
      Guestbooks (12)
      Image Manipulation (21)
      Installing PHP (7)
      Introduction to PHP (23)
      Link Indexing (8)
      Mailing List Management (9)
      Miscellaneous (53)
      Networking (8)
      News Publishing (9)
      OOP (24)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (6)
      Postcards (1)
      Randomizing (14)
      Redirection (11)
      Searching (9)
      Security (29)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot