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

Link to comment
Share on other sites

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? 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.