Jump to content

search no working properly


james182

Recommended Posts

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'])){
}

Link to comment
https://forums.phpfreaks.com/topic/152923-search-no-working-properly/
Share on other sites

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>";
						}
					}

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.