473,804 Members | 3,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trouble escaping / Misc nightmare

Sorry for the double question. I'm having a terrible time figuring out
how to escape apostrophes in my mySQL database. Perhaps they have to
be escaped in the PHP, using mysql_real_esca pe_string?

This is the code:

http://www.gongfamily.net/code.txt

The page in question is:

http://www.gongfamily.net/daevid.php

the entry being N'existe Pas. Information about the album should show
up on the left when the title is clicked on, but it doesn't happen
whenever there's an apostrophe. This is an escaping problem, I'm sure,
but the real problem is, I'm over my head!

The second problem is on the same page. You can click a table header
to sort the table, but when I click the 'Group' header, I get the
error:

"You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'Group ASC' at line 1"

But I'm lost as to what to do here. I've poured over the code, despite
not being very well-versed in PHP. Again, I'm over my head, and would
truly appreciate any help.

TIA

Ian
--
Was it not a comedy, a strange and stupid
matter, this repetition, this running around
in a fateful circle? (Hermann Hesse)
http://www.bookstacks.org/
Jul 17 '05
14 4118
Ken Robinson (ke******@rbnsn .com) wrote:
: Malcolm Dew-Jones wrote:
: > Ken Robinson (ke******@rbnsn .com) wrote:
: > : You can also use urlencode($var) or htmlentities($v ar,ENT_QUOTES)
: > : before inserting $var into your database.
: >
: > You can, but you should still use mysql_escape_st ring on the result when
: > you embed it in an sql query being handled by mysql.

: You learn something new all the time. We you retrieve a string that
: was stored this way, does MySQL unescape it or is there a function to
: do it?

You do not need to unescape the string when you retrieve it later.

When you use mysql_escape_st ring then it ensures that no unexpected
characters in the value can corrupt the SQL command. This ensures that
the value seen by the database is the correct value, i.e. the original,
unescaped data.

e.g.

$value = "this', 'will mess things up";

$sql1 = "insert into tbl values ('$value')"; # inserts 2 columns!

$esc_value = mysql_escape_st ring($value);

$sql2 = "insert into tbl values ('$esc_value')" ; # this is correct

If you were to now examine the contents of the database then you would see
that the second insert will have inserted a value into just one column,
and the string stored in that column will be

this', 'will mess things up

which is the original contents of the $value variable, i.e. when you
retrieve the value then you do not need to unescape it.

This has nothing to do with escaping it for html of course. If you wish
to use the value later, such as displaying it in a web page, then you
might want to escape it for that task before your use it. If you know you
are only ever going to display the data in a web page then you might wish
to do the html escaping before you save the data so that it is always
displayable in a web page with no additional steps. However, when you
save the html-escaped data in the database then you will still wish to
ensure that it is saved correctly, which is what mysql_escape_st ring is
for.

Often mysql_escape_st ring is not necessary since the data may not have any
' or ; or any other odd characters, but using it for all string values is
a good habit that will save you from unexpected problems in the future
when some one manages to enter some data that is not what you anticipated.

--

This space not for rent.
Jul 17 '05 #11
On 28 Jun 2005 19:01:02 -0800, yf***@vtn1.vict oria.tc.ca (Malcolm
Dew-Jones) wrote:
$value = "this', 'will mess things up";

$sql1 = "insert into tbl values ('$value')"; # inserts 2 columns!

$esc_value = mysql_escape_st ring($value);

$sql2 = "insert into tbl values ('$esc_value')" ; # this is correct


Hi Malcolm. From my palty understanding of PHP, :-), it seems as if
you're talking about inserting values into a database. The values I'm
worried about are already in there. I use phpMyAdmin to build my
database, and I can see when I browse the table in question that the
apostrophes aren't causing a problem.

The HTML/PHP prints out the string just fine on the web page
(apostrophe and all). This is a string which can be clicked on, at
which point the browser sends the text of the link back to the
database, asking for a record with that string. It doesn't find it,
because the apostrophe is messing things up somehow. I would guess
this is a query somewhere in my HTML/PHP document, perhaps:

$album=mysql_es cape_string($_G ET['album']);

or

$query_rsDaevid = "SELECT * FROM daevid ORDER BY $sort_order ASC";

Nothing seems to help, though, and this is probably a simple thing (to
someone else, at least). :-) Sorry if I'm taking up too much space on
the ng. Just developing a real headache over this.

Ian
--
Was it not a comedy, a strange and stupid
matter, this repetition, this running around
in a fateful circle? (Hermann Hesse)
http://www.bookstacks.org/
Jul 17 '05 #12
Ian Rastall wrote:
On 28 Jun 2005 12:32:57 -0700, "saint exupery"
<cr************ ***@gmail.com> wrote:
'Group' is an invalid name for a column! (mysql get confused with the
"GROUP BY" clause).


LOL. Thank you so much. That would probably not have occurred to me in
a hundred years! :-)


When in doubt what my php/mysql combo is trying to do, I restart
mysql with "--log=/tmp/dumplog" option, and then can see what exactly is
passed to mysql from the script. After that, I can fire away the mysql
client, and copy/paste queries, and investigate at leisure.

/m
Jul 17 '05 #13
you cant grab it cuz the apostrophe is escaped on the db.

you MUST escape the string before adding it to the db using
addslashes():
mysql_query("IN SERT INTO `blabla` (`a`,`b`) VALUES ('" .
addslashes($var 1) . "','" . addslashes($var 2) . "')");

use stripslashes() when outputting to html:
echo stripslashes($r esult['b'])

u should be fine with that. in theory, you should never urlencode()
when outputting or urldecode() when inputting (it`s the browser`s job
to do that)

Jul 17 '05 #14
Ian Rastall (id*******@gmai l.com) wrote:
: On 28 Jun 2005 19:01:02 -0800, yf***@vtn1.vict oria.tc.ca (Malcolm
: Dew-Jones) wrote:

: > $value = "this', 'will mess things up";
: >
: > $sql1 = "insert into tbl values ('$value')"; # inserts 2 columns!
: >
: > $esc_value = mysql_escape_st ring($value);
: >
: > $sql2 = "insert into tbl values ('$esc_value')" ; # this is correct

: Hi Malcolm. From my palty understanding of PHP, :-), it seems as if
: you're talking about inserting values into a database.

My examples were inserts, but any data put into a query string should be
escaped so the string is interpretted correctly. The escaping ensures the
string contains the original data (not the escaped data) when it arrives
at the database.

e.g.
$id = "somebody's data";
$escaped_id = mysql_escape_st ring($id);
$sql = "select * from tbl where id='$escaped_id '";

: The values I'm
: worried about are already in there. I use phpMyAdmin to build my
: database, and I can see when I browse the table in question that the
: apostrophes aren't causing a problem.

It's hard to know with 100% certainty what is in the database because
phpMyAdmin will have had to escape the data to display it. I would want
to use mysql> to confirm the characters are what I think they are. (And I
wonder if the character set can make a difference, the character may look
like a ' but is it the same binary value as your data that looks like a '
(?)
: The HTML/PHP prints out the string just fine on the web page
: (apostrophe and all).

My question is whether the apostrophe you see is stored as an apostrophe
(') or as something else, such as &apos; which will end up looking
correct in the browser, depending on what phpMyAdmin does when displaying
the data.
: This is a string which can be clicked on, at
: which point the browser sends the text of the link back to the
: database, asking for a record with that string. It doesn't find it,
: because the apostrophe is messing things up somehow.

The query from the browser must correctly encode the ' too. Again, the
string could actually be something else and just look like a '.
I am thinking I would open a temp text file and store all the values you
are receiving at each stage you use them, so you can examine the bytes
later in a text editor.

# PSEUDO code

fp = fopen("/tmp/my-file.txt","w");

$album = $_GET['album'];
printf(fp,'$alb um = $_GET['album'] => [%s]\n",$album);

$album=mysql_es cape_string($al bum);
printf(fp,'mysq l_escaped $album = [%s]\n",$album);

$sql = "select * from .etc.etc ";
printf(fp,'the sql string=[%s]\n",$sql);

# etc, also dump the data from the database same way
: Nothing seems to help, though, and this is probably a simple thing (to
: someone else, at least). :-) Sorry if I'm taking up too much space on
: the ng. Just developing a real headache over this.

The niggly little things are always the trickiest.
--

This space not for rent.
Jul 17 '05 #15

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

Similar topics

4
4421
by: Dave Moore | last post by:
Hi All, Can anybody point me to a FAQ or similar that describes what all this stuff is about please?. I'm interfacing with a MySQL database if that's relavent. I've read a couple of books which refer to stripslahes and 'escaping' but nothing really explains what these terms are and why these are used. Why is 'escaping' (whatever that is) used?. What the hell is a magic quote?. How is it different from a non-magic one?. Regards, Dave
5
6348
by: bobbyballgame | last post by:
I am having a problem calling Stored Procedures: .... dim MyValue, MyOtherValue MyValue = "Bobby's value" MyOtherValue = Bobby's other value" rs.Open "exec MyStoredProc """ & MyValue & """, """ & MyOtherValue & """", Conn
1
16111
by: Weston C | last post by:
In the course of trying to build a simple clock, I've run into a problem using the setInterval (and setTimeout) function. http://weston.canncentral.org/misc/tkeep/tkeep.html http://weston.canncentral.org/misc/tkeep/tkeep.jss function fieldToClock(fieldId) { var field = document.getElementById(fieldId); alert("Starting a clock in text field " + fieldId + "(" + field
4
15621
by: 21novembre | last post by:
Hi all, I got a quite strange problem when I tried to setup a database backup shell. I put it this way: "bin/mysqldump --opt --user=xxx --password=xxx DB > DB.bak" However, error 1045 came to me to say "Access denied for user 'xxx'@'localhost' (using password: YES) when trying to connect". None the less, I'm absolutely full of confidence on my correct username and password, simply because if I do it this way: "bin/mysqldump --opt...
2
1746
by: weston | last post by:
So, I'm attempting to code an expanding tree menu, based off of unordered lists containing unordered lists. I'm also trying to do it in such a way that none of the javascript has to go inline with the markup: http://weston.canncentral.org/misc/webgallery/FMH/template.html So far, so good. It seems to work in Gecko-based browsers rather well (mouse-over "Online Services" to see it work).
11
2190
by: Geoff Caplan | last post by:
Hi folks, The thread on injection attacks was very instructive, but seemed to run out of steam at an interesting point. Now you guys have kindly educated me about the real nature of the issues, can I ask again what effective escaping really means? Are the standard escaping functions found in the PHP, Tcl etc APIs to Postgres bombproof? Are there any encodings that might slip through and be cast to malicious strings inside Postgres?...
9
2000
by: =?Utf-8?B?Sm9obiBCYWlsZXk=?= | last post by:
I have a ASP .Net page that allows moving around items on the page through javascript. This page works fine in IE. In FireFox however, I have found that if the page is using XHTML 1.0 Transitional as the doctype, you cannot set the style.left and style.top properties of image or div tags. If you remove the doctype from the page it works just fine, although I would rather not do this. You can work around this by setting the cssText...
3
3382
by: placid | last post by:
Hi All, I have these files; which are Merge Request (ClearCase) files that are created by a Perl CGI script (being re-written in Python, as the HTML/ JavaScript have been mixed with Perl, maintainability is zero) MergeType::::codefromlabel:::: BLname::::BUILDMODS:::: OldLname:::::::: BaseVersion::::6.9.1.24A::::
1
5486
by: David Henderson | last post by:
I know 'disable-output-escaping' has been discussed in the past, but I can't put my finger on any of the threads to see if my current problem is addressed. Sorry for re-asking the question if it has already been answered... I have an XML doc that I am transforming via XSLT and JavaScript in the browser. This allows me to return unsorted data to the browser and allow the user to sort it with a mouseclick and not hit the server just...
0
9706
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
10578
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...
0
10077
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
9152
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4300
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
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
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.