dazz_club Posted February 26, 2008 Share Posted February 26, 2008 Hi guys and girls, I am working on a project and I would like to give the user the facility to search for a product on my website. If anyone has any urls to good tutorials then that would be great. Here is the html code for my form <form name="form1" id="form1" method="POST" action="searchDataBase.php"> <ul style="list-style-type:none;"> <li><input type="text" name="searchterm" id="search" ></li> <li><input type="image" name="submit" value="submit!" src="images/search-bttn.png" id="submit" title="Click here to search"></li> </form> and here is the scripti have for searchDataBase.php <?php require_once("includes/connection.php"); ?> <?php $query= "select * from products where $searchtype like '%$searchterm%'"; $reslut = mysqli_query($db, $query); ?> Below is how i have structured my products table products ---------------- product_id name type image category ----------- I have about 26 products in this table so far. Kind regards Dazzclub Link to comment https://forums.phpfreaks.com/topic/93116-create-a-search-function-for-my-website/ Share on other sites More sharing options...
synstealth Posted February 26, 2008 Share Posted February 26, 2008 you have undefined varibale $searchtype <-- is it called from $_POST['searchtype']; ?? Link to comment https://forums.phpfreaks.com/topic/93116-create-a-search-function-for-my-website/#findComment-477039 Share on other sites More sharing options...
pocobueno1388 Posted February 26, 2008 Share Posted February 26, 2008 It looks like your off to the right start. Change the searchDataBase.php to this: <?php require_once("includes/connection.php"); $searchterm = mysql_real_escape_string($_POST['searchterm']); //I don't see an input in your form named "searchtype", So I'm not sure where this is coming from $searchtype = mysql_real_escape_string($_POST['searchtype']); $query= "SELECT * FROM products WHERE $searchtype LIKE '%$searchterm%'"; $result = mysqli_query($db, $query)or die(mysql_error() . "<p>With query:<br>$query"); while ($row = mysql_fetch_assoc($result)){ echo $row['name'].'<br>'; } ?> I put a comment in the code, I wasn't sure where you pulling a variable from. Link to comment https://forums.phpfreaks.com/topic/93116-create-a-search-function-for-my-website/#findComment-477040 Share on other sites More sharing options...
The Little Guy Posted February 26, 2008 Share Posted February 26, 2008 This works well: <?php $query_count = "SELECT SQL_CALC_FOUND_ROWS title, content, URL, id FROM $searchType WHERE MATCH(URL,title,content) AGAINST ('$searchQuery' IN BOOLEAN MODE) AND content IS NOT NULL LIMIT $limitvalue, $limit"; $sql = mysql_query($query_count)or die(mysql_error()); $result_count = mysql_query("SELECT FOUND_ROWS()")or die(mysql_error()); $total = mysql_fetch_array($result_count); $totalrows = $total[0]; // returns ALL found rows if LIMIT wasn't there ?> Replace the columns and tables as yours. Link to comment https://forums.phpfreaks.com/topic/93116-create-a-search-function-for-my-website/#findComment-477047 Share on other sites More sharing options...
dazz_club Posted February 26, 2008 Author Share Posted February 26, 2008 @pocobueno1388 Thanks for the reply, and thanks for the little bit of incouragement i thought i was goin to some harsh feedback in regards to what i have wrttin so far, code wise. That variable you questioned i believe was left there by accident, originally i had to input fields but i thought it would be best to have one. I have ran the script and this message is being displayed code of html form <form name="form1" id="form1" method="POST" action="searchDataBase.php"> <ul style="list-style-type:none;"> <li><input type="text" name="searchterm" id="search" ></li> <li><input type="image" name="submit" value="submit" src="images/search-bttn.png" id="submit" title="Click here to search"></li> </form> searchDataBase.php script <?php require_once("includes/connection.php"); $searchterm = mysql_real_escape_string($_POST['searchterm']); $query= "SELECT * FROM 'products' WHERE $searchterm LIKE '%$searchterm%'"; $result = mysqli_query($db, $query)or die(mysql_error() . "<p>With query:<br>$query"); while ($row = mysql_fetch_assoc($result)){ echo $row['name'].'<br>'; } ?> Warning: mysqli_query() expects parameter 1 to be mysqli, null given in E:\wamp\www\drinkpromo\searchDataBase.php on line 7 With query: SELECT * FROM products WHERE LIKE '%dazzclub%' What have i done wrong??? Link to comment https://forums.phpfreaks.com/topic/93116-create-a-search-function-for-my-website/#findComment-477131 Share on other sites More sharing options...
nethnet Posted February 26, 2008 Share Posted February 26, 2008 Your WHERE clause has no table column listed. If you are using pocobueno's code then the reason it isn't working is because there is no element named "searchtype" in your form. You'll have to add that. Theo Link to comment https://forums.phpfreaks.com/topic/93116-create-a-search-function-for-my-website/#findComment-477134 Share on other sites More sharing options...
dazz_club Posted February 26, 2008 Author Share Posted February 26, 2008 Hi nethnet, I have now ammended the form so that the additional field no longer exists. my new code now looks like. (searchDataBase.php) <?php require_once("includes/connection.php"); $search = mysql_real_escape_string($_POST['search']); $query= "SELECT * FROM 'products' WHERE $search LIKE '%$searchterm%'"; $result = mysqli_query($db, $query)or die(mysql_error() . "<p>With query:<br>$query"); while ($row = mysql_fetch_assoc($result)){ echo $row['name'].'<br>'; } ?> and the form which is submitting the searchterm is <form name="form1" id="form1" method="POST" action="searchDataBase.php"> <ul style="list-style-type:none;"> <li><input type="text" name="search" id="search" ></li> <li><input type="image" name="submit" value="submit" src="images/search-bttn.png" id="submit" title="Click here to search"></li> </form> Hope this helps Link to comment https://forums.phpfreaks.com/topic/93116-create-a-search-function-for-my-website/#findComment-477273 Share on other sites More sharing options...
nethnet Posted February 26, 2008 Share Posted February 26, 2008 I'm confused by your script. Your form is submitted to searchDataBase.php and the search query from the form is set to the variable $search. So what exactly are your users searching through? From the looks of it, you're allowing your users to search through your database structure. In your SQL: "SELECT * FROM 'products' WHERE $search LIKE '%$searchterm%'" There are a few errors. If your customers are searching for a product name, for example, you'd need to rearrange your SQL a little bit: "SELECT * FROM `products` WHERE `name` LIKE '%$search%'" That way, the $search variable is used to search for the actual name of the product, and not for a column in your database called '$search'. This is, of course, assuming you have a column named 'name' with the products name stored in it. Theo Link to comment https://forums.phpfreaks.com/topic/93116-create-a-search-function-for-my-website/#findComment-477279 Share on other sites More sharing options...
dazz_club Posted February 26, 2008 Author Share Posted February 26, 2008 hmm sorry for all the confusion ok, hopefully i can explain it better. i have search form, when works, will pass the variable search to searchDatabase.php which will then, using the chosen search term, go through the products table and out put all the products that matche the users term. does this help? sorry if i am annoying you. Link to comment https://forums.phpfreaks.com/topic/93116-create-a-search-function-for-my-website/#findComment-477287 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.