473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Simple php sql question

I need some help. I want to know using php sql to use OR in a query.

Let me explain in detail:

I have a html form which asks the user for country,state,city,zip_code

I will use these values to query the database for specific data for those specific inputs.

I want the city and zip_code to be logicly use as OR. Meaning that the user only needs to select a city or type the exact zip_code. Either one should still be able to grab the data from the database.

Here is the code I used:
Code:
Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM postings  
  3. WHERE state ='".$state."' AND Country ='".$country."' 
  4.   AND city ='".$city."' OR zipcode ='".$zipcode."'
  5.   AND item ='".$item."' 
  6. LIMIT $s,$limit"
I tried to use Where(first values goes here meaning with city value) Or (value of zip code);

when I tried that code concept. I get a white screen with an error saying the syntax is wrong where limit starts at.


The query is supposed to grab all data based on that users form inputs. I am creating a paging effect. That is why I use the limit code.

Yet I want the user to input either the city name or zip_code.

how should I be doing this? I played around with the code but got nothing right now.

Currently the code only works when you input the country,state,city name. It won't allow to have the city name blank and use a zip code.
May 11 '19 #1
2 1002
Luuk
1,047 Expert 1GB
"when I tried that code concept. I get a white screen with an error saying the syntax is wrong where limit starts at."
It would be nice if you showed the statement that you actually tried, and the error it gave.

You are using 'item' in your where clause, and you are not referring to in in your question above.

Next solution might be what you are looking for (given above 'notes'):
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM postings  
  2. WHERE 
  3.   (
  4.     (
  5.       ( state ='".$state."' AND 
  6.         Country ='".$country."' AND 
  7.         city ='".$city."' 
  8.       )
  9.       OR 
  10.         zipcode ='".$zipcode."' 
  11.     )
  12.     AND 
  13.       item ='".$item."' 
  14.    )
  15. LIMIT $s,$limit"
May 11 '19 #2
You can use 'mysqli_error' (or similar) PHP function to get a more meaningful error message. Given the limited information in your question I can't be sure where the error comes from, but here are a few possibilities:

1. 'item' is not an SQL column. If so, then you need to remove it from the query.

2. Either $s or $limit variable does't actually contain a valid number.

3. The $item variable contains a string with a quote mark in it. If so, then you need to at least replace "'" with "\'" in that string, e.g: $s=str_replace("'","\\'",$s). And in general, you should do this (and more) for every string you get from a user, otherwise an attacker can hack your entire database by filling your form with SQL injection code.

On a more general note, you should require the 'country' column even if zip is provided instead of a city, since different countries can have identical zip codes for different cities, so a zip code is't sufficient to identify a country and a city.
May 11 '19 #3

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

Similar topics

7
by: gene.ellis | last post by:
Good morning. I am racking my brains over what seems like should be a simple question. I have a string that contains text and html. Basically, I would like to grab the HTML tags from the string and...
2
by: Kirk | last post by:
A very simple question for anyone who knows their HTML tags and attributes. Is there an atribute that I can use to hide the white space around a table which I have used to fill an entire page? ...
2
by: Anurag | last post by:
This simple one beats me all ends up(sincerely). I have been doing DB2 UDB for some time now, reading a lot of good discussions in this forum, writing some answers, asking a lot more but this...
3
by: Peter | last post by:
Hello Thanks for reviewing my question. I would like to know how can I programmatically select a node Thanks in Advanc Peter
1
by: Chris | last post by:
Sorry to ask such a simple question but here it is, and I'm am new to ASP/WEB I am designing a site and I want to make it general so I can easily change the font/color/sizes of the...
3
by: Brad | last post by:
I have another hopefully simple question. I am so used to writing VB .Net windows apps that there are some things in ASP .Net that just does not easily cross over. I know how to pass variables to...
7
by: abcd | last post by:
I am trying to set up client machine and investigatging which .net components are missing to run aspx page. I have a simple aspx page which just has "hello world" printed.... When I request...
2
by: Allain Bøge | last post by:
It is really a simple question. Visual Basic .NET (2003) I create 2 forms (Form1 and Form2) I create a checkbox in Form1 (checkbox1) I create a checkbox in Form2 (checkbox1) I go to Form1...
13
by: Saber | last post by:
I did a lot of searches and read something about datagrids. But I couldn't find the answer of my simple question, how can I show only my desired columns of a table? for example I wrote this sql...
17
by: AlBen | last post by:
Hello sorry I don't know about javascript but I have to finish my work and there I have some scripts on my page I have a textarea form and a select form when a user click in the select form...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
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
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,...

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.