473,657 Members | 2,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HELP: pesky SQL syntax error using PHP variables

Hello:

New user here...first post to group.

I'm getting an SQL syntax error when I try to run the following query:

$query = sprintf("SELECT itemNumber, entryDate, modifyDate, thumbnailURL,
title, price FROM '%s' WHERE itemNumber = '%s'", $_POST['selectCategory '],
$_POST['tfItemNum']);

I've tested the variables and they are populated.

Strangely, the query works with a variable in the WHERE clause, but not in
the FROM clause.

Any tips appreciated,

Frank H
Austin, TX
Jul 10 '06 #1
11 1896
Frankie wrote:
Hello:

New user here...first post to group.

I'm getting an SQL syntax error when I try to run the following query:

$query = sprintf("SELECT itemNumber, entryDate, modifyDate, thumbnailURL,
title, price FROM '%s' WHERE itemNumber = '%s'", $_POST['selectCategory '],
$_POST['tfItemNum']);

I've tested the variables and they are populated.

Strangely, the query works with a variable in the WHERE clause, but not in
the FROM clause.

Any tips appreciated,

Frank H
Austin, TX

Your FROM clause would be FROM 'foo'. It should be FROM foo instead.

Try:
$query = sprintf("SELECT itemNumber, entryDate, modifyDate,
thumbnailURL, title, price FROM %s WHERE itemNumber = '%s'",
$_POST['selectCategory '], $_POST['tfItemNum']);

-david-

Jul 10 '06 #2
Thank you soooo much!

Those darn quotes...so confusing. I'm delirious from staring at this query
all morning.

F.H.

"David Haynes" <da***********@ sympatico.cawro te in message
news:pv******** ***********@fe7 6.usenetserver. com...
Your FROM clause would be FROM 'foo'. It should be FROM foo instead.

Try:
$query = sprintf("SELECT itemNumber, entryDate, modifyDate,
thumbnailURL, title, price FROM %s WHERE itemNumber = '%s'",
$_POST['selectCategory '], $_POST['tfItemNum']);

Jul 10 '06 #3
Rik
Frankie wrote:
Thank you soooo much!

Those darn quotes...so confusing. I'm delirious from staring at this
query all morning.
Rule of thumb:
backticks(``) around names derived from the database (fields, database,
tables)
quotes('') around strings

Grtz,
--
Rik Wasmus
Jul 10 '06 #4
Frankie wrote:
Hello:

New user here...first post to group.

I'm getting an SQL syntax error when I try to run the following query:

$query = sprintf("SELECT itemNumber, entryDate, modifyDate, thumbnailURL,
title, price FROM '%s' WHERE itemNumber = '%s'", $_POST['selectCategory '],
$_POST['tfItemNum']);

I've tested the variables and they are populated.

Strangely, the query works with a variable in the WHERE clause, but not in
the FROM clause.

Any tips appreciated,

Frank H
Austin, TX

I hope you're checking those $_POST variables before blindly making the
SQL call!

Robin
Jul 11 '06 #5

"Robin" <an**@somewhere .comwrote in message
news:e8******** **@gemini.csx.c am.ac.uk...
>
<snip>

I hope you're checking those $_POST variables before blindly making the
SQL call!
Yes, $_POST['selectCategory '] comes from a select menu while
$_POST['tfItemNum'] is checked by "ereg", and then again by "strip_tags " if
re-displayed.

Thanks again to all who responded.

F.H.

Jul 12 '06 #6
Frankie wrote:
"Robin" <an**@somewhere .comwrote in message
news:e8******** **@gemini.csx.c am.ac.uk...
>><snip>

I hope you're checking those $_POST variables before blindly making the
SQL call!


Yes, $_POST['selectCategory '] comes from a select menu while
$_POST['tfItemNum'] is checked by "ereg", and then again by "strip_tags " if
re-displayed.

Thanks again to all who responded.

F.H.
By "comes from a select menu" do you mean is the product of a <select
name="selectCat egory"tag?

You cannot guarantee that this value will only be one of your <option>
tag values. Posted data is easily forged.

Robin
Jul 13 '06 #7
----- Original Message -----
From: "Robin" <an**@somewhere .com>
Newsgroups: comp.lang.php
Sent: Thursday, July 13, 2006 3:25 AM
Subject: Re: HELP: pesky SQL syntax error using PHP variables

><snip>

You cannot guarantee that this value will only be one of your <option>
tag values. Posted data is easily forged.
Hmmm.

So you're suggesting all POST data be cleaned, even if it comes from a
select menu which doesn't allow user input? In this case, a bogus POST value
would only cause the query to fail, right? Or could a malicious user gain
access to the server this way?

At the moment, I only clean data that allows direct user input, such as text
fields.

F.H.
Jul 14 '06 #8
Rik
Frankie wrote:
----- Original Message -----
From: "Robin" <an**@somewhere .com>
Newsgroups: comp.lang.php
Sent: Thursday, July 13, 2006 3:25 AM
Subject: Re: HELP: pesky SQL syntax error using PHP variables

><snip>

You cannot guarantee that this value will only be one of your
<optiontag values. Posted data is easily forged.

Hmmm.

So you're suggesting all POST data be cleaned, even if it comes from a
select menu which doesn't allow user input?
Yes. I could send raw headers to your script, but much simpler is to make my
own form with the apropriate names, and post it to your url...
In this case, a bogus
POST value would only cause the query to fail, right?
Nope.
Or could a
malicious user gain access to the server this way?
Yes and no. If the POST values are used for db queries, one could pretty
much do anything to your database very easily if you haven't protected
yourself against it. Depending on how the rest of your server is setup, and
how sensitive data is kept, maybe even more.
At the moment, I only clean data that allows direct user input, such
as text fields.

You should check, escape & clean all data that comes from the users.

Grtz,
--
Rik Wasmus
Jul 14 '06 #9
Frankie wrote:
----- Original Message -----
From: "Robin" <an**@somewhere .com>
Newsgroups: comp.lang.php
Sent: Thursday, July 13, 2006 3:25 AM
Subject: Re: HELP: pesky SQL syntax error using PHP variables
>><snip>

You cannot guarantee that this value will only be one of your <option>
tag values. Posted data is easily forged.

Hmmm.

So you're suggesting all POST data be cleaned, even if it comes from a
select menu which doesn't allow user input? In this case, a bogus POST value
would only cause the query to fail, right? Or could a malicious user gain
access to the server this way?

At the moment, I only clean data that allows direct user input, such as text
fields.
As Rik says, trust nothing that has come from the user as users cannot
be trusted! Don't rely on the standard way browsers restrict the posted
data (such as select tags), or even javascript validation before submission.

Note: (often forgotten) that $_SERVER['PHP_SELF'] comes from the user
too and can be used for cross site scripting attacks (see
http://en.wikipedia.org/wiki/XSS ).

Robin
Jul 14 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2508
by: Ted | last post by:
I've written a little function to remove everything after the 2nd decimal place for prices which is as follows: - ReturnConvertedCurrency = (fix(iSterling * session("ExchangeRate") * 100) / 100) However, it sometimes returns incorrect values. i.e. Why does the following: - response.write(FormatNumber((fix(2.30 * 1 * 100) / 100) , 2))
8
5462
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
8
17491
by: Dalan | last post by:
Please help - just take a quick look at the function code below. It probably just needs some minor tweaking. The function module is based on an intermediate query to provide a group record total from a subform that is posted to a field on the main form. "Everything seems to work fine" except that I consistently receive an error everytime I attempt to create a new record on the main form. However, there are no errors received when...
3
3118
by: Jerry | last post by:
Well, here is some weirdness. First, I noticed that I have 2 Set keywords (silly me). so I removed the 2nd "Set" but still got a syntax error. Then I removed the Where clause, and now it works perfectly. Is this correct? Or am I just getting lucky? I'm not completely clear on the fundamentals here. I update the table in my dataset, then I update the table on the server through the dataAdapter. I think I have a handle on the...
16
1754
by: danu | last post by:
I have a structure : typedef struct{ char magicNum; int width; int height; int maxGrey; int pixels; } ImageT;
1
3705
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online. Plz....Plz..Plz...
3
1359
by: Jon | last post by:
All, I'm currently building a custom Content Management system for a site we're working on, and am stuck. Currently, I am using a couple of classes to run most of the queries throughout the application. Well, I'm pretty stuck now. What I need to do is use a variable throughout my classes that is a Session variable. I really can't find another solution. The syntax I was using for this variable before (I actually hard coded it during my...
6
3346
by: AppleBag | last post by:
I'm having the worst time trying to login to myspace through code. Can someone tell me how to do this? Please try it yourself before replying, only because I have asked this a couple of times in the past in other places, and while the help was much appreciated, it seemed everyone just wanted to 'theoretically' explain how to do it, but when I tried to do it myself, I couldn't login. I want to simply pass the email address and password to...
1
2004
by: mshroom12 | last post by:
Hello to all. I am having difficulty trying to do this Java project using Eclipse. The following is what I have to do. Election Day It's almost election day and the election officials need a program to help tally election results. There are two candidates for office -- Polly Tichen and Ernest Orator. The program's job is to take as input the number of votes each candidate received in each voting precinct and find the total number of votes for...
0
8385
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8303
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8821
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8502
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8602
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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 we have to send another system
2
1941
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.