473,508 Members | 2,226 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP Match two fields in a row via my search

47 New Member
Hi

I have entered data into my table and now I want to retrieve it via a search engine on my site:

fourwaysdp.co.uk/seaside.php

I have two search boxes because I want to only retrieve rows that match the search terms.

I want each search box to represent each column in my table.

At the moment if i search: 'trainee' in one box and 'london' in the other all rows with trainee are displayed even if they do not have london in the row.

Here is the code:

Expand|Select|Wrap|Line Numbers
  1. <form action="seaside.php" method="post">
  2. <input type="text" name="search">
  3. <input type="text" name="search2">
  4. <input type="submit">
  5. </form>
  6. <hr width="100%"></hr>
  7.  
  8. <?php
  9. if(strlen(trim($_POST['search'])) > 0) {
  10. //all of your php code for the search
  11.  
  12.    $search = "%" . $_POST["search"] . "%";
  13.  
  14.   mysql_connect ("", "", "");
  15.   mysql_select_db ("");
  16.  if (!empty($_POST["search_string"])) 
  17.    { 
  18.  
  19.    }  
  20.   $query = "SELECT name,location,msg FROM contact WHERE name LIKE '$search'";
  21.   $result = mysql_query ($query);
  22.   if ($result) {
  23.     while ($row = mysql_fetch_assoc($result)) {
  24.       echo "{$row['name']}<br>".
  25.            "{$row['location']}<br>".
  26.            "{$row['msg']}<br>";
  27.  
  28.     }
  29.   }
  30. }
  31. ?>
  32. </body>
  33. </html>
  34.  
Thanks!

James
Aug 19 '11 #1
1 1454
dlite922
1,584 Recognized Expert Top Contributor
First why do you have 2 search boxes? is one for Location and one for name column?

Your query is right. But you're ignoring $_POST['search2']?? Why aren't you also grabbing that.

Finally, look into SQL Injection. If this forum is available on the internet anyone can hack it and destroy your Database. Use the function mysql_real_escape_string() to give you a clean input and prevent injection.

what is $_POST["search_string"]) suppose to do? I don't see an input named search_string.

Cheers,


Dan
Aug 19 '11 #2

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

Similar topics

6
1936
by: Ward Cleaver | last post by:
Hi. I have an assignment to do some validating of a form using javascript and mostly the search() method. I'm having problems getting a positive validation for phone numbers like "123-456-7890"...
0
1201
by: doug | last post by:
I am doing a search now where I have a statement "FirstName LIKE '%$firstname%' OR JobTitle LIKE '%$jobtitle%' how can I do the same statement using full text search, for example with fulltext I...
20
2370
by: coosa | last post by:
Dear all, I have illustared with code and sample output data my request in thsi post. I simply was expecting some results from my search "amd socket a 32 bit cache 512 dell" that includes a logical...
3
2200
by: Liz Malcolm | last post by:
Hello and TIA for guidance. I am building a reusable search procedure (thanks go to Graham Thorpe for his example that set me on my way). Everything works up until the 2nd match is found, the...
7
2601
by: spike | last post by:
Im writing a program to search for a string in a binary file. And it works. The problem is: It is sooo slow! how can i make it faster? It takes 27 seconds just to search a 5 meg file. I guess it...
0
3041
by: NateDawg | last post by:
I would like to show all the data in a table to users, they can then select from a drop down list the column they would like to search. Then enter the criteria they would like to search by. When...
3
2917
by: balakrishnan.dinesh | last post by:
Hi frnds, Im having a problem in searching out a particular substring in a string. Here the code: --------------------- <html> <script> var tmpString=" ";
1
1678
by: Lance Hoffmeyer | last post by:
Hey all, I have a search: VAR = re.search("PROVEN.*?\n+\d(.*?)\n.*?" , pcpT9, re.S ).group(1) #.split() that does not match (sometimes it will and sometimes it will not match) Traceback...
1
1232
by: Lance Hoffmeyer | last post by:
Hey all, How do I add another index inside a re.search? I want to add for j in BASE = re.search ... sh.Cells97... for i ....
2
1645
by: sovixi | last post by:
Hi I want to search a text file (.txt) for words that I specify in my program. However, I can’t come up with any working solution. My program search string in a text file for ‘met|meeting|meets’ but...
0
7383
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
7046
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
5627
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,...
1
5053
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
4707
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
3194
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
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1557
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 ...
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.