james182 Posted April 7, 2009 Share Posted April 7, 2009 i am trying to search my DB if i type a word it's fine but if i use a space between words like hello world, i don't get anything even if i know that there is data to display. Below is my preg_match statement for checking, i need to allow spaces. if(preg_match('|^[a-zA-Z0-9!@#$%^&*();:_.\\\\ /\t-]+$|', $_POST['name'])){ } Quote Link to comment https://forums.phpfreaks.com/topic/152923-search-no-working-properly/ Share on other sites More sharing options...
imperium2335 Posted April 7, 2009 Share Posted April 7, 2009 explode the user input using a space as the separator and put them into an array, then have it loop through the words the user entered to check if any are in your database, if so return the result. Off top of my head. Quote Link to comment https://forums.phpfreaks.com/topic/152923-search-no-working-properly/#findComment-803137 Share on other sites More sharing options...
james182 Posted April 7, 2009 Author Share Posted April 7, 2009 here is my full search code: <div id="global_search"> <div id="search_form"> <p>Search for: Clients, Jobs, Members, Documents, and more.</p> <form action="<?php site_url();?>search/8/true/" method="POST" > <p>Find:<input type="text" name="name" value="" class="searchField" /> <input type="hidden" name="name_store" value="<?php echo $_POST['name']?>" class="searchField" /> <select name="tblSearch" class="searchSelect"> <option value="tbl_clients" selected>Clients</option> <option value="tbl_jobdata">Jobs</option> <option value="tbl_users">Members</option> <option value="tbl_docs">Documents</option> </select> <input type="submit" name="submit" value="Search" class="searchbtn" /></p> </form> </div> </div> <?php if(isset($_POST['submit'])){ field_validator("name", $_POST['name'], "alphanumeric", 2, 150); echo "begin: ". $_POST['name'] ."<br />"; if($messages){ //showForm(); echo "Type more then 2 charaters"; exit; } if(isset($_GET['go'])){ echo "<div id='search_results'>"; if(preg_match('|^[a-zA-Z0-9!@#$%^&*();:_.\\\\ /\t-]+$|', $_POST['name'])){ $q = $_POST['name']; $db_tbl = $_POST['tblSearch']; echo "after: ". $_POST['name'] ."<br />"; global $link; switch($db_tbl){ case "tbl_clients": $query = "SELECT * FROM tbl_clients WHERE client_name LIKE '%" . $q . "%' OR client_postal_address LIKE '%" . $q ."%' OR client_street_address LIKE '%" . $q ."%' OR client_industry LIKE '%" . $q ."%' "; $result = mysql_query($query, $link) or die("Search Clients fatal error: ".mysql_error()); echo "<ul>"; while($r = mysql_fetch_array($result)){ ?> <li><a href="<?php site_url() ?>search_detail/8/<?php echo $db_tbl ?>/<?php echo $r["client_id"] ?>/<?php echo $_POST['name'] ?>/">» <?php echo $r["client_name"]; ?></a><br> <b>Contact:</b> <?php echo $r["client_contact"] ?> ~ <b>Industry:</b> <?php echo $r["client_industry"] ?></li> <?php } echo "</ul>"; break; case "tbl_jobdata": $query = "SELECT * FROM tbl_jobdata WHERE job_name LIKE '%" . $q . "%' OR job_number LIKE '%" . $q ."%' OR job_office LIKE '%" . $q ."%' "; $result = mysql_query($query, $link) or die("search fatal error: ".mysql_error()); echo "<ul>"; while($r = mysql_fetch_array($result)){ ?> <li><a href="<?php site_url() ?>search_detail/8/<?php echo $db_tbl ?>/<?php echo $r["job_id"] ?>/">» <?php echo $r["job_name"] ?></a> <br /> <b>Job #:</b> <?php echo $r["job_number"] ?> ~ <b>Leader:</b> <?php echo $r["job_leader"] ?> ~ <b>Office:</b> <?php echo $r["job_office"] ?></li> <?php } echo "</ul>"; break; case "tbl_users": $query = "SELECT * FROM tbl_users WHERE user_firstname LIKE '%" . $q . "%' OR user_surname LIKE '%" . $q ."%' ORDER BY user_firstname ASC "; $result = mysql_query($query, $link) or die("search fatal error: ".mysql_error()); echo "<ul>"; while($r = mysql_fetch_array($result)){ $user_firstname = $r["user_firstname"]; $user_surname = $r["user_surname"]; $user_id = $r["user_id"]; ?> <li><a href="<?php site_url();?>account/3/<?php echo $user_id ?>/user/">» <?php echo $user_firstname ?> <?php echo $user_surname ?></a><br /> <b>Email:</b> <a href="mailto:<?php echo $r["user_email"] ?>"><?php echo $r["user_email"] ?></a> ~ <b>EXT:</b> <?php echo $r["user_extension"] ?></li> <?php } echo "</ul>"; break; case "tbl_docs": $query = "SELECT * FROM tbl_docs WHERE doc_name LIKE '%" . $q . "%' OR doc_tags LIKE '%" . $q ."%' "; $result = mysql_query($query, $link) or die("search fatal error: ".mysql_error()); echo "<ul>"; while($r = mysql_fetch_array($result)){ $doc_name = $r["doc_name"]; $doc_tags = $r["doc_tags"]; $doc_id = $r["doc_id"]; ?> <li><a href="<?php site_url() ?>doc_view/<?php echo $r["doc_id"] ?>/">» <?php echo $doc_name ?></a><br /> <b>Description:</b> <?php echo $r["doc_description"] ?><br /> <b>Tags:</b> <?php echo $r["doc_tags"] ?></li> <?php } echo "</ul>"; break; } }else{ echo "<p>Please enter a search query</p>"; } echo "</div>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/152923-search-no-working-properly/#findComment-803139 Share on other sites More sharing options...
gizmola Posted April 7, 2009 Share Posted April 7, 2009 I don't see a problem with the regex. The question is, what do you do after you get into your loop? How are you attempting to search the database? What database are you using, and what is the SQL syntax you're using? Quote Link to comment https://forums.phpfreaks.com/topic/152923-search-no-working-properly/#findComment-803142 Share on other sites More sharing options...
james182 Posted April 7, 2009 Author Share Posted April 7, 2009 Look at my code thats the whole thing Quote Link to comment https://forums.phpfreaks.com/topic/152923-search-no-working-properly/#findComment-803147 Share on other sites More sharing options...
gizmola Posted April 7, 2009 Share Posted April 7, 2009 Yeah you posted that just before I hit enter I guess. Without scrutinizing too much of your code, in the case of name search, this is clearly not going to work, because you apparently have a first name and last name, however, you are not attempting to break the name into its constituent parts. In that case you need to do something -- explode it on the space would be one way to handle it. Quote Link to comment https://forums.phpfreaks.com/topic/152923-search-no-working-properly/#findComment-803150 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.