Jump to content

Creating a search form that will search a SQL DB


ciaran1987

Recommended Posts

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

 

<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

 

<?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

 

<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?

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.