473,385 Members | 1,813 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,385 software developers and data experts.

dynamic SQL built in a PHP form - problem with character values

40
I have a multi-field search PHP page, and the resulting PHP page builds dynamic WHERE statements to do the database search. The first field is numeric, and works:
Expand|Select|Wrap|Line Numbers
  1. $facility = $_REQUEST['fac_id'];
  2. if ($facility != '') {
  3. echo "Facility: ".$facility." ";
  4. $where .= ' AND fac_id='.$facility.'';
  5. }
  6.  
This code builds SQL that looks like this:
SELECT fac_id, room_descr, person_type, severity FROM incidents WHERE 1=1 AND fac_id=000852 ORDER BY fac_id, room_descr, person_type, severity.
But when the field is character, this same code does not work:
Expand|Select|Wrap|Line Numbers
  1. $persontype = $_REQUEST['person_type'];
  2. if ($persontype != '') {
  3. echo "Persontype: ".$persontype. " ";
  4. $where .= ' AND person_type ='.$persontype.'';
  5. }
  6.  
This produces the following SQL:
SELECT fac_id, room_descr, person_type, severity FROM incidents WHERE 1=1 AND person_type =Outpatient ORDER BY fac_id, room_descr, person_type, severity
And gives the error:
Error: Unknown column 'Outpatient' in 'where clause'.
If I copy the SQL produced and add quotes around Outpatient it runs fine in phpmyadmin.

How do I add single quotes to the person_type field?

TIA,

jej1216
Jun 5 '07 #1
2 1476
jej1216
40
I got it.

Expand|Select|Wrap|Line Numbers
  1. $persontype = $_REQUEST['person_type'];
  2. if ($persontype != '') {
  3. echo "Persontype: ".$persontype. " ";
  4. $where .= " AND person_type ='".$persontype."'";
  5. }
  6.  
I knew it was a small thing.

jej1216
Jun 5 '07 #2
pbmods
5,821 Expert 4TB
Good thinking.

One thing I must caution you about, though: What if $_REQUEST['person_type'] were "'\cDROP TABLE `incidents`"?

http://us2.php.net/addslashes
Jun 5 '07 #3

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

Similar topics

10
by: Dave Karmens | last post by:
If I have say 10 fixed variables, how can I set their values = to that of a form that is built dynamically? column1 column2 email = formvalue(0) fname = formvalue(1) lname = formvalue(2)...
7
by: Jack | last post by:
Hi, I am trying to test a sql statement in Access which gives me the error as stated in the heading. The sql statement is built as a part of asp login verification, where the userid and password...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
3
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which...
0
by: MattB | last post by:
Hi. I'm on my second redesign of a dynamic form I need to create and I'm looking for good examples and/or suggestions. I have a web application that is being distributed to different clients of...
2
by: deejayquai | last post by:
Hi I'm trying to produce a report based on a dynamic crosstab. Ultimately i'd like the report to actually become a sub report within a student end of year record of achievement. The dynamic...
15
by: Kapil Jain | last post by:
Dear All, What i need to achieve is : I am generating dynamic text boxes thru dhtml coding, i need onChange event of oragnistation text box i.e dynamically generated on click of "More" button in...
5
by: jdzemke | last post by:
I am getting 'options' is null or not an object using when using a dynamic dataset with a dropdown list in an html form. I am filling-in a text field and filtering the values in the drop-down. ...
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.