473,465 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

multiple search

28 New Member
Hi! I'm trying to do a multiple search in my php code. I have an input text field where I input my wanted values then when I click the search button, An html table would show all my searched values from my database table.

For example, I want to search multiple names(mark, jake, john, michael) <-- this is typed in a single text field separated by commas.

Then it will go to another page which will show all records from my database having a name of "mark", "jake", "john", or "michael".


This is my initial code (page afrer clicking the search button) result.php:

Expand|Select|Wrap|Line Numbers
  1. $search = $_POST["search"];
  2. $array_search = explode(',', $search);
  3. $imploded_search = implode(",", $array_search);
  4.  
  5. $query = mysql_query("SELECT * FROM users WHERE name IN($imploded_search)");
  6.  
  7. while ($row = mysql_fetch_array($query)) 
  8. {
  9.     echo $row['name'];
  10. }
  11.  
Nothing seems to display :(
When I added '' in $imploded_search in
Expand|Select|Wrap|Line Numbers
  1. $query = mysql_query("SELECT * FROM users WHERE name IN('$imploded_search')"); 
It only searches for ONE name which is the first name I have inputted. ("mark")

I believe that $imploded_search creates one string 'mark, jake, john, michael' =
Expand|Select|Wrap|Line Numbers
  1. $query = mysql_query("SELECT * FROM users WHERE name IN('mark, jake, john, michael')"); 
and it needs to become 'mark', 'jake', 'john', 'michael' in order for it to work.
Expand|Select|Wrap|Line Numbers
  1. $query = mysql_query("SELECT * FROM users WHERE name IN('mark', 'jake', 'john', 'michael')"); 

I need help on how to do this.
THANK YOU VERY MUCH in advance!
Apr 27 '12 #1
4 1896
Dormilich
8,658 Recognized Expert Moderator Expert
as you have already found out, you need to enclose each element of the array by quotes. why not using implode() for that (as implode() also adds the commas)?

besides the fact that you’re wide open to SQL Injection ...
Apr 27 '12 #2
roshan ban
21 New Member
Create a simple array that can store a search word in array after parsing the special seperator that you have place like (, or ; etc)
then make a search in a loop one after another simultaneously
Apr 29 '12 #3
migi48
28 New Member
Hello guys thanks for the replies,

@Dormilich - That's what I'm actually trying to do. But unfortunately, I cannot figure out how can I enclose each element of the array by quotes using implode. Please teach me how to do that. Sorry for being new to PHP functions.

@roshan ban - Are you suggesting to make a loop of SQL querries? I believe that it would only make the system slower?
Apr 30 '12 #4
deric
92 New Member
Try this:

Expand|Select|Wrap|Line Numbers
  1. $array_search = explode(',', $search);
  2. $imploded_search = "'".implode("','", $array_search)."'";
Apr 30 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Vishy | last post by:
Hi One of the less documented features of xpath is how to run multiple search on nodes or attributes e.g <Book name="Harry Potter" price="10GBP"/> <Book name="Harry Potter" price="5USD"/> now...
1
by: TH | last post by:
I am (still :) working on a recipe database. Now I am trying to figure out how to set it up for an ingredient search. What I want it to be able to do is three things: 1. Search based on just...
2
by: Alphonse Giambrone | last post by:
Is there a way to use multiple search patterns when calling Directory.GetFiles. For instance Directory.GetFiles("C:\MyFolder", "*.aspx") will return all files with the aspx extension. But what if...
1
by: Casey | last post by:
Hi, How would one allow for possible multiple search criteria using FREETEXTTABLE. For example, my table "listings" has a full-text search catalog, and I may want to: SELECT * FROM listings...
2
by: mukeshsrivastav | last post by:
dear sir, i m design a contact mgmt interface in vb using access . i want to have multiple search option ,suppose i have four field ,first name ,lastname ,city ,state. the client can enter this...
3
by: AishaKhalfan | last post by:
Hi, :wave: I have many search criteria in my program, such as: search by name, search by age, search by gender, search by city the user can search by name only, or by name and age, or by...
11
by: Panlflzs | last post by:
Howdy, I am working on a system where I need a basic HTML form to pass data to a cfm page. The cfm page will then query a database and return any matching data. I am using MySQL. I have my HTML...
1
by: geetaarora2003 | last post by:
i need to find nodes which satisifies multiple search string in xml file using asp.net
5
by: Grubsy4u | last post by:
Hi all, I have recently created a list box in a form.....I wanted to know if there was a way of searching for multiple records within the list box. How can i get this list box to search for...
1
by: josil | last post by:
Hi I'm a newbie in this and need help. I mange to make a one field search form. But now I want to go further. How can I make a multiple field form? <h2>Search</h2>
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.