Jump to content

search box


bigrossco

Recommended Posts

Hi

 

I am wanting to make a search box that will search the input data from the database.

 

What I am wanting is to be able to enter a client id number i.e 2001 and it will display the information for that client.

 

I used to have the code to do this but sadly lost it :(

 

All I am wanting is one search box on a page to input the number and when you click submit it shows the users info.

 

Thanks

 

Ross

Link to comment
Share on other sites

I'm assuming you just want the PHP. Something like this would do it:

 

<?php
/*
Assuming a MySQL database
*/
mysql_connect('127.0.0.1:3306', 'user', 'pass') or
   die('Could not connect: ' . mysql_error());
mysql_select_db('database');

/*
You'll have a variable ($search_query here) with the value of the query string
*/

$result = mysql_query("SELECT clientid, field1, field2, field3 FROM table_name WHERE clientid LIKE '$search_query' ");
?>

Link to comment
Share on other sites

thanks how would i get the search box to pass the inputed text into the '$search_query' variable

 

it's pretty simple. basically, you have an HTML form, for instance:

<form action="" method="post">
<input type="text" name="input_field_1">
<input type="text" name="input_field_2">
<input type="text" name="input_field_3">
<input type="submit" value="SUBMIT">

 

the way php processes the values of these text fields is by using the $_POST method. for instance, if you wanted to print these values elsewhere you could use:

<?php
        echo "first input field: ". $_POST['input_field_1'] ."<br />\n";
        echo "second input field: ". $_POST['input_field_2'] ."<br />\n";
        echo "third input field: ". $_POST['input_field_3'] ."<br />\n";
?>

 

that make sense?

Link to comment
Share on other sites

ok makes sence

 

for the text input to a field, I want it to redirect to another page with that variable i.e. result.php?id=inputfield1 how would i do this?

 

you will be unable to do that directly from the form. the form action="" must have the url where you want the form values to be sent to. except, you won't get all of the information you need, until the user has submitted the information. so, what you can do, is use a redirect page.

 

basically, you will have a form like this:

<form action="redirect.php" method="post">
<input type="text" name="input_field_1">
<input type="text" name="input_field_2">
<input type="text" name="input_field_3">
<input type="submit" value="SUBMIT">
</form>

 

and your redirect page will process the posted information and forward to the page you want with the posted values in the url, like this:

<?php
        /*redirect.php*/

        header("Location: http://www.yoursite.com/result.php?id=". $_POST['input_field_1'] ."");
?>

Link to comment
Share on other sites

got it working thanks but now a bit more advanced thing i am wanting to work :)

 

I have 1 input and 1 dropdown box, when a user clicks submit it shouild go to a page where it shows data from a mysql database from the data from the input where its equal to the selected item on the dropdown this is the code i have :

 

    $query = "SELECT *

FROM clients WHERE '".$_SESSION["method"]."' = '".$_SESSION["search"]."'";

 

 

for the sql quiry, when i chose method as ID it dont work but when i change the code to:

 

    $query = "SELECT *

FROM clients WHERE id = '".$_SESSION["search"]."'";

 

and but the id as search it dose work :S

 

any ideas?  i think its something to do with where i have  '".$_SESSION["method"]."'

 

i do have

 

<?php
<?php


$_SESSION['method'];
if (isset ($_GET['method'])){
$_SESSION['method'] = $_GET['method'];
}


$_SESSION['search'];
if (isset ($_GET['search'])){
$_SESSION['search'] = $_GET['search'];
}
?> 

 

at the header

Link to comment
Share on other sites

i want the drop down box to refer to the coloum on the database i.e. id, phone number, name and the text box to refer to the input

 

ok for that you would first, populate the dropdown menu by the contents of the sql table:

<?php
        $db = mysql_connect("xxx", "xxx", "xxx");
        mysql_select_db("your_db", $db);

        $sql = "SELECT * FROM your_table";
        $query = mysql_query($sql);

        echo "<form action=\"redirect.php\" action=\"post\">\n";
        echo "<select name=\"dropdown\">\n";

        while($row = mysql_fetch_array($query)){
                echo "<option value=\"". $row['column1'] ."\">". $row['column2'] ."\n";
        }

        echo "</select>\n";
        echo "<input type=\"submit\" value=\"Submit\">\n";
?>

 

does that make sense?

then, to refer to the exact row in the database that the user has selected from the dropdown menu:

<?php
        $db = mysql_connect("xxx", "xxx", "xxx");
        mysql_select_db("your_db", $db);

        $sql = "SELECT * FROM your_table WHERE column1 = '". $_POST['dropdown'] ."'";
        $query = mysql_query($sql);

        while($row = mysql_fetch_array($query)){
                echo "<p>Column1: ". $row['column1'] ."</p>\n";
                echo "<p>Column2: ". $row['column2'] ."</p>\n";
                echo "<p>Column3: ". $row['column3'] ."</p>\n";
                echo "<p>Column4: ". $row['column4'] ."</p>\n";
        }
?>

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.