bigrossco Posted February 26, 2007 Share Posted February 26, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/40174-search-box/ Share on other sites More sharing options...
SpeedBird Posted February 26, 2007 Share Posted February 26, 2007 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' "); ?> Quote Link to comment https://forums.phpfreaks.com/topic/40174-search-box/#findComment-194393 Share on other sites More sharing options...
bigrossco Posted February 26, 2007 Author Share Posted February 26, 2007 thanks how would i get the search box to pass the inputed text into the '$search_query' variable Quote Link to comment https://forums.phpfreaks.com/topic/40174-search-box/#findComment-194398 Share on other sites More sharing options...
boo_lolly Posted February 26, 2007 Share Posted February 26, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/40174-search-box/#findComment-194426 Share on other sites More sharing options...
bigrossco Posted February 27, 2007 Author Share Posted February 27, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/40174-search-box/#findComment-195193 Share on other sites More sharing options...
boo_lolly Posted February 27, 2007 Share Posted February 27, 2007 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'] .""); ?> Quote Link to comment https://forums.phpfreaks.com/topic/40174-search-box/#findComment-195256 Share on other sites More sharing options...
bigrossco Posted February 27, 2007 Author Share Posted February 27, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/40174-search-box/#findComment-195488 Share on other sites More sharing options...
boo_lolly Posted February 27, 2007 Share Posted February 27, 2007 dude, i'm sorry, but i have no idea what you mean by your last post. i mean... i didn't understand a thing... you gotta elaborate. Quote Link to comment https://forums.phpfreaks.com/topic/40174-search-box/#findComment-195501 Share on other sites More sharing options...
bigrossco Posted February 28, 2007 Author Share Posted February 28, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/40174-search-box/#findComment-196046 Share on other sites More sharing options...
itsmeArry Posted February 28, 2007 Share Posted February 28, 2007 Can you give the structure of table means the datatypes for id, phone number, name that will help get more info about you problem... Quote Link to comment https://forums.phpfreaks.com/topic/40174-search-box/#findComment-196077 Share on other sites More sharing options...
boo_lolly Posted February 28, 2007 Share Posted February 28, 2007 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/40174-search-box/#findComment-196141 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.