shaunno2007 Posted August 17, 2008 Share Posted August 17, 2008 I have this form <form id="submit-search-query" name="submit-search-query" method="get" action=""> <label> <center><input name="search" type="text" class="show-search-box-text-box" size="20"/> </center> </label> <label> <center><input name="submit-search" type="submit" class="show-search-box-submit-button" value="Search" /> </center> </label> </form> and i want it to search my database and echo out the search results that have to word what was entered in the form. Can anyone help thanks Shaun Quote Link to comment https://forums.phpfreaks.com/topic/120083-making-a-form-thats-searches-database/ Share on other sites More sharing options...
ngreenwood6 Posted August 17, 2008 Share Posted August 17, 2008 You would have to set the action to another page maybe search.php. Your search.php would look something like this <?php $host = "mysqlhost"; $user = "user"; $pass = "pass"; $db = "db"; $table = "table" $search = $_POST['search']; $connect = mysql_connect($host, $user, $pass); mysql_select_db($db); $query = "SELECT * from $table WHERE search"; //You would want to put in your specific search here! if(!$search) { echo "Please enter a search"; } else { $result = mysql_query($query); } while($row = mysql_fetch_array($result)) { extract($row) echo $row['data']; //you would change data to the data that you want displayed. } ?> This is kind of a rough draft but you should get the point and can modify for your needs. Quote Link to comment https://forums.phpfreaks.com/topic/120083-making-a-form-thats-searches-database/#findComment-618607 Share on other sites More sharing options...
shaunno2007 Posted August 17, 2008 Author Share Posted August 17, 2008 thanks for your reply i will have a play around with it now Quote Link to comment https://forums.phpfreaks.com/topic/120083-making-a-form-thats-searches-database/#findComment-618610 Share on other sites More sharing options...
ngreenwood6 Posted August 17, 2008 Share Posted August 17, 2008 No prob. Glad to help out. Quote Link to comment https://forums.phpfreaks.com/topic/120083-making-a-form-thats-searches-database/#findComment-618618 Share on other sites More sharing options...
shaunno2007 Posted August 17, 2008 Author Share Posted August 17, 2008 You would have to set the action to another page maybe search.php. Your search.php would look something like this <?php $host = "mysqlhost"; $user = "user"; $pass = "pass"; $db = "db"; $table = "table" $search = $_POST['search']; $connect = mysql_connect($host, $user, $pass); mysql_select_db($db); $query = "SELECT * from $table WHERE search"; //You would want to put in your specific search here! if(!$search) { echo "Please enter a search"; } else { $result = mysql_query($query); } while($row = mysql_fetch_array($result)) { extract($row) echo $row['data']; //you would change data to the data that you want displayed. } ?> This is kind of a rough draft but you should get the point and can modify for your needs. Got a problem... Its saying Parse error: syntax error, unexpected T_ECHO in C:\wamp\www\bizlizard\link-search.co.nr new design\index.php on line 89 I don't know why i am getting this. Quote Link to comment https://forums.phpfreaks.com/topic/120083-making-a-form-thats-searches-database/#findComment-618775 Share on other sites More sharing options...
redarrow Posted August 17, 2008 Share Posted August 17, 2008 try this <?php $host = "mysqlhost"; $user = "user"; $pass = "pass"; $db = "db"; $table = "table"; $search = $_POST['search']; $connect = mysql_connect($host, $user, $pass); mysql_select_db($db); $query = "SELECT * from $table WHERE search"; //You would want to put in your specific search here! if(!$search) { echo "Please enter a search"; } else { $result = mysql_query($query); } while($row = mysql_fetch_array($result)) { extract($row); echo $row['data']; //you would change data to the data that you want displayed. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/120083-making-a-form-thats-searches-database/#findComment-618799 Share on other sites More sharing options...
shaunno2007 Posted August 18, 2008 Author Share Posted August 18, 2008 try this <?php $host = "mysqlhost"; $user = "user"; $pass = "pass"; $db = "db"; $table = "table"; $search = $_POST['search']; $connect = mysql_connect($host, $user, $pass); mysql_select_db($db); $query = "SELECT * from $table WHERE search"; //You would want to put in your specific search here! if(!$search) { echo "Please enter a search"; } else { $result = mysql_query($query); } while($row = mysql_fetch_array($result)) { extract($row); echo $row['data']; //you would change data to the data that you want displayed. } ?> hmm... now i am getting this... Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\bizlizard\link-search.co.nr new design\index.php on line 85 Don't know why tho. Could i some how make it so it goes to another page like so... someone submits the form and then it sends the data and the user to search.php and the results are there? Quote Link to comment https://forums.phpfreaks.com/topic/120083-making-a-form-thats-searches-database/#findComment-618838 Share on other sites More sharing options...
chronister Posted August 18, 2008 Share Posted August 18, 2008 Ok, you can't use the code verbatim. There are a couple spots where you need to change it to make it work for YOU and YOUR SITUATION. first spot..... $query = "SELECT * from $table WHERE search"; You need to make it say something like "SELECT * FROM $table WHERE $fieldName LIKE '%$search%'" Notice the 3 variables there.... $table is set earlier and should be the name of the table your searching, $fieldName is one I added, replace that with the name of the field your looking in. If you want to look in a field called usernames, then replace it with that. The LIKE %$search% part is a wildcard search pattern. if the letter A was typed in the search field it woud find things like Another (a is the first letter), Panda (a is in the middle and at the end), Antarctica (a is in the beginning, middle and end) , should those things be in the database. the % is the wild card, $row['data'] needs to be $row['yourFieldName'] meaning replace that with the name of the field your searching in. Hope this helps some. Quote Link to comment https://forums.phpfreaks.com/topic/120083-making-a-form-thats-searches-database/#findComment-618868 Share on other sites More sharing options...
shaunno2007 Posted August 18, 2008 Author Share Posted August 18, 2008 Ok, you can't use the code verbatim. There are a couple spots where you need to change it to make it work for YOU and YOUR SITUATION. first spot..... $query = "SELECT * from $table WHERE search"; You need to make it say something like "SELECT * FROM $table WHERE $fieldName LIKE '%$search%'" Notice the 3 variables there.... $table is set earlier and should be the name of the table your searching, $fieldName is one I added, replace that with the name of the field your looking in. If you want to look in a field called usernames, then replace it with that. The LIKE %$search% part is a wildcard search pattern. if the letter A was typed in the search field it woud find things like Another (a is the first letter), Panda (a is in the middle and at the end), Antarctica (a is in the beginning, middle and end) , should those things be in the database. the % is the wild card, $row['data'] needs to be $row['yourFieldName'] meaning replace that with the name of the field your searching in. Hope this helps some. This part i don't understand. you can't use the code verbatim. There are a couple spots where you need to change it to make it work for YOU and YOUR SITUATION. first spot..... $query = "SELECT * from $table WHERE search"; You need to make it say something like "SELECT * FROM $table WHERE $fieldName LIKE '%$search%'" This part i don't understand. The LIKE %$search% part is a wildcard search pattern. This oark i do understand i have done this i will show you my name for the tables field names and the database name. $row['data'] needs to be $row['yourFieldName'] meaning replace that with the name of the field your searching in. Hope this helps some./quote] The database name is "bizlizard" The table name is "websites" And the field name is "url" Could you, if its not too much trouble, put it all together for me so i can see how it all goes. (Together)(If you want) Thank you very much Shaun Quote Link to comment https://forums.phpfreaks.com/topic/120083-making-a-form-thats-searches-database/#findComment-618893 Share on other sites More sharing options...
shaunno2007 Posted August 18, 2008 Author Share Posted August 18, 2008 hope it help you to understand what i mean Quote Link to comment https://forums.phpfreaks.com/topic/120083-making-a-form-thats-searches-database/#findComment-619245 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.