laflair13 Posted March 4, 2015 Share Posted March 4, 2015 (edited) This has me pulling my hair out! I have tried everything I could find and it is still displaying all the items in the database. I have a search area on my site and when I put in a search keyword, the results.php page has every item on it. I have searched and searched and I cannot figure out why it is doing it. Any help would be greatly appreciated. My search form <form id="search-form123" action="results.php" method="POST"> <div class="offlajn-ajax-search-inner"> <input type="text" name="keyword" id="search-area123" value="" autocomplete="off" placeholder="Search Here..."> <input type="submit" name="Submit" value="Search" id="search-area123"> <input type="hidden" name="Submit" value="com_search"> </form> results.php ( on the top of page ) <?php include_once('mysql_connect.php'); if (!isset($_POST['search'])) $keyword = $_POST['search']; $search_sql="SELECT * FROM new_equip WHERE itemname LIKE '%" .$keyword. "%'"; $search_query=mysql_query($search_sql); if(mysql_num_rows($search_query)!=0) { $search_rs=mysql_fetch_assoc($search_query); } $eid = $row['id']; $itemname = $row['itemname']; ?> results.php ( where the items are displayed ) <?php if (mysql_num_rows($search_query)!=0){ do {?> <p><a href="new-product.php?Item=<?php echo $search_rs['id']; ?>"><?php echo $search_rs ['itemname']?></a></p> <?php } while ($search_rs=mysql_fetch_assoc($search_query)) ; } else { echo "No Results Found"; } ?> And I will say that I am learning mysqli to convert my site over. I am just wanting to do it all at once so there are no issues. If anyone can help with that, I would greatly appreciate it because again I have search and tried everything and I cant get it working. Edited March 4, 2015 by laflair13 Quote Link to comment https://forums.phpfreaks.com/topic/295105-simple-search-feature-displays-all-items/ Share on other sites More sharing options...
Q695 Posted March 4, 2015 Share Posted March 4, 2015 DON'T use mysql_* either use PDO, or mysqli Quote Link to comment https://forums.phpfreaks.com/topic/295105-simple-search-feature-displays-all-items/#findComment-1507543 Share on other sites More sharing options...
laflair13 Posted March 4, 2015 Author Share Posted March 4, 2015 I know, I stated that I am learning it and I will convert the site in a whole when I get it. I dont want to just do a few pages here and there. Quote Link to comment https://forums.phpfreaks.com/topic/295105-simple-search-feature-displays-all-items/#findComment-1507549 Share on other sites More sharing options...
Barand Posted March 4, 2015 Share Posted March 4, 2015 $keyword = $_POST['search']; You don't have an input with name = search so you end up with "LIKE '%%' " which is everything Quote Link to comment https://forums.phpfreaks.com/topic/295105-simple-search-feature-displays-all-items/#findComment-1507556 Share on other sites More sharing options...
laflair13 Posted March 4, 2015 Author Share Posted March 4, 2015 (edited) Thank you for your response but wouldnt this get passed as the input search? on form <input type="text" name='keyword' id="search-area123" value="" autocomplete="off" placeholder="Search Here..."/> Then on results.php if (!isset($_POST['search'])) $keyword = $_POST['search']; Edited March 4, 2015 by laflair13 Quote Link to comment https://forums.phpfreaks.com/topic/295105-simple-search-feature-displays-all-items/#findComment-1507557 Share on other sites More sharing options...
Barand Posted March 4, 2015 Share Posted March 4, 2015 Inputs are posted by name, so you need $keyword = $_POST['keyword']; Quote Link to comment https://forums.phpfreaks.com/topic/295105-simple-search-feature-displays-all-items/#findComment-1507559 Share on other sites More sharing options...
laflair13 Posted March 4, 2015 Author Share Posted March 4, 2015 I tried changing it and it is still displaying all the items. form <input type="text" name='keyword' id="search-area123" value="" autocomplete="off" placeholder="Search Here..."/> <input type="submit" name='Submit' value="Search" id="search-area123" /> <input type="hidden" name='Submit' value="com_search" /> results.php if (!isset($_POST['keyword'])) $keyword = $_POST['keyword']; $search_sql="SELECT * FROM new_equip WHERE itemname LIKE '%" .$keyword. "%'"; $search_query=mysql_query($search_sql); if(mysql_num_rows($search_query)!=0) { $search_rs=mysql_fetch_assoc($search_query); } Quote Link to comment https://forums.phpfreaks.com/topic/295105-simple-search-feature-displays-all-items/#findComment-1507562 Share on other sites More sharing options...
mac_gyver Posted March 4, 2015 Share Posted March 4, 2015 just a minor point, but your search form is specifying what will be displayed/gotten on the page and should use the get method. this will make it easier to propagate and combine the search/keyword term in url's since it will already be a $_GET parameter. Quote Link to comment https://forums.phpfreaks.com/topic/295105-simple-search-feature-displays-all-items/#findComment-1507563 Share on other sites More sharing options...
laflair13 Posted March 4, 2015 Author Share Posted March 4, 2015 (edited) Ok, I just changed the method="POST" to method="$_GET" and I got this in the url results.php?keyword=capper&Submit=Search&Submit=com_search Edited March 4, 2015 by laflair13 Quote Link to comment https://forums.phpfreaks.com/topic/295105-simple-search-feature-displays-all-items/#findComment-1507565 Share on other sites More sharing options...
CroNiX Posted March 5, 2015 Share Posted March 5, 2015 I think it was fine using POST. I think the problem was here in your results.php: if (!isset($_POST['keyword'])) $keyword = $_POST['keyword']; //... You're saying if the $_POST['keyword'] IS NOT SET, to USE $_POST['keyword'] which obviously you can't since it doesn't exist Try removing the ! from the isset (which makes it NOT isset()) Quote Link to comment https://forums.phpfreaks.com/topic/295105-simple-search-feature-displays-all-items/#findComment-1507581 Share on other sites More sharing options...
laflair13 Posted March 5, 2015 Author Share Posted March 5, 2015 Still getting all the items showing up. <?php include_once('mysql_connect.php'); if (isset($_POST['keyword'])) $keyword = $_POST['keyword']; $search_sql="SELECT * FROM new_equip WHERE itemname LIKE '%" .$keyword. "%'"; $search_query=mysql_query($search_sql); if(mysql_num_rows($search_query)!=0) { $search_rs=mysql_fetch_assoc($search_query); } $eid = $row['id']; $itemname = $row['itemname']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/295105-simple-search-feature-displays-all-items/#findComment-1507583 Share on other sites More sharing options...
CroNiX Posted March 5, 2015 Share Posted March 5, 2015 Try wrapping the whole thing in braces so it will only execute if $_POST['keyword'] exists. if (isset($_POST['keyword'])) { //add brace $keyword = $_POST['keyword']; $search_sql="SELECT * FROM new_equip WHERE itemname LIKE '%" .$keyword. "%'"; $search_query=mysql_query($search_sql); if(mysql_num_rows($search_query)!=0) { $search_rs=mysql_fetch_assoc($search_query); } $eid = $row['id']; $itemname = $row['itemname']; } //add brace Quote Link to comment https://forums.phpfreaks.com/topic/295105-simple-search-feature-displays-all-items/#findComment-1507585 Share on other sites More sharing options...
CroNiX Posted March 5, 2015 Share Posted March 5, 2015 Also, if you put this at the very TOP of results.php for debugging echo '<pre>'; print_r($_POST); echo '</pre>'; die(); What do you get? Quote Link to comment https://forums.phpfreaks.com/topic/295105-simple-search-feature-displays-all-items/#findComment-1507586 Share on other sites More sharing options...
laflair13 Posted March 5, 2015 Author Share Posted March 5, 2015 Warning: mysql_num_rows() expects parameter 1 to be resource, null given in results.php on line 255No Results Found if (mysql_num_rows($search_query)!=0){ When I add the debugging code I get Array() Quote Link to comment https://forums.phpfreaks.com/topic/295105-simple-search-feature-displays-all-items/#findComment-1507587 Share on other sites More sharing options...
CroNiX Posted March 5, 2015 Share Posted March 5, 2015 That means your form is incorrect somehow. Show us your entire form, including the form open/close tags. Did you change the form back to using POST? also, if you implemented the braces I showed you you shouldn't get that error you mentioned, unless you didn't show us all of the code. Quote Link to comment https://forums.phpfreaks.com/topic/295105-simple-search-feature-displays-all-items/#findComment-1507588 Share on other sites More sharing options...
laflair13 Posted March 5, 2015 Author Share Posted March 5, 2015 Form code <form id="search-form123" action='results.php' method="POST"> <div class="offlajn-ajax-search-inner"> <input type="text" name='keyword' id="search-area123" value="" autocomplete="off" placeholder="Search Here..."/> <input type="submit" name='Submit' value="Search" id="search-area123" /> <input type="hidden" name='Submit' value="com_search" /> <div id="search-area-close123"></div> <div id="ajax-search-button123"><div class="magnifier"></div></div> <div class="ajax-clear"></div> </div> </form> Top of results.php <?php include_once('mysql_connect.php'); if (isset($_POST['keyword'])) { //add brace $keyword = $_POST['keyword']; $search_sql="SELECT * FROM new_equip WHERE itemname LIKE '%" .$keyword. "%'"; $search_query=mysql_query($search_sql); if(mysql_num_rows($search_query)!=0) { $search_rs=mysql_fetch_assoc($search_query); } $eid = $row['id']; $itemname = $row['itemname']; } //add brace ?> Where results are displayed <?php if (mysql_num_rows($search_query)!=0){ do {?> <p><a href="new-product.php?Item=<?php echo $search_rs['id']; ?>"><?php echo $search_rs ['itemname']?></a></p> <?php } while ($search_rs=mysql_fetch_assoc($search_query)) ; } else { echo "No Results Found"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/295105-simple-search-feature-displays-all-items/#findComment-1507589 Share on other sites More sharing options...
Solution laflair13 Posted March 5, 2015 Author Solution Share Posted March 5, 2015 Well after A LOT of trial and error I figured out how to make it work in mysqli. If you could, please double check my work to make sure it is a good way to do it? form <form id="search-form123" action='results.php' method="GET"><input type="text" name='keyword' id="search-area123" value="" autocomplete="off" placeholder="Search Here..."/> <input type="submit" name='Submit' value="Search" id="search-area123" /> <input type="hidden" name='Submit' value="com_search" /> </form> Top of results.php <?php $db = new mysqli("localhost","admin","pass","database"); if(!$db) { die('sorry we are having some problbems'); } // SET GETTER AS A VARIABLE $searchTerm = mysqli_real_escape_string($db,$_GET['keyword']); if ( empty($searchTerm)) { echo("no key words searched please try again"); } else { $sql = mysqli_query( $db, sprintf( "SELECT * FROM new_equip WHERE itemname LIKE '%s'", '%'. $searchTerm .'%' ) ); } ?> where results are displayed <?php while($ser = mysqli_fetch_array($sql)) { echo "<p><a href='new-product.php?Item=$ser[id]'>$ser[itemname]</a></p>"; } ?> I want to say thank you to everyone who took the time with the help on this. I as lost. Quote Link to comment https://forums.phpfreaks.com/topic/295105-simple-search-feature-displays-all-items/#findComment-1507590 Share on other sites More sharing options...
Barand Posted March 5, 2015 Share Posted March 5, 2015 Ok, I just changed the method="POST" to method="$_GET" and I got this in the url results.php?keyword=capper&Submit=Search&Submit=com_search The form method should be "GET" and not "$_GET" (although you get away with probably as GET is the default). Having changed the form, $_GET is what you should now be using in the php code instead of $_POST Quote Link to comment https://forums.phpfreaks.com/topic/295105-simple-search-feature-displays-all-items/#findComment-1507603 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.