473,395 Members | 2,423 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,395 software developers and data experts.

addslashes, mysql_real_escape_string, etc not working

110 100+
I am finding this very strange and frustrating, but I've got some data being entered into a mysql database, and when the data contains an apostrophe for example the word we're, it shows up in the database as
we’re.

weird.

I've used addslashes & mysql_real_escape_string, but nothing seems to fix it, and I can see when I 'echo' the input, that the string is not being escaped.


I don't get any errors, but nothing happens.
Expand|Select|Wrap|Line Numbers
  1.     preg_match ("/<input type=\"hidden\" name=\"Title\" value=\"([^`]*?)\">/", $match, $temp);
  2.     $Title = $temp['1'];
  3.     $Title = trim($Title);
  4.     $Title =addslashes($Title); 
  5.  
  6.  echo $Title.'<br />';
  7.  
May 10 '08 #1
8 3994
I am finding this very strange and frustrating, but I've got some data being entered into a mysql database, and when the data contains an apostrophe for example the word we're, it shows up in the database as
we’re.

weird.

I've used addslashes & mysql_real_escape_string, but nothing seems to fix it, and I can see when I 'echo' the input, that the string is not being escaped.


I don't get any errors, but nothing happens.
Expand|Select|Wrap|Line Numbers
  1.     preg_match ("/<input type=\"hidden\" name=\"Title\" value=\"([^`]*?)\">/", $match, $temp);
  2.     $Title = $temp['1'];
  3.     $Title = trim($Title);
  4.     $Title =addslashes($Title); 
  5.  
  6.  echo $Title.'<br />';
  7.  
I'm not that sure, but I do have cases like that in another programming language. Turns out that my page is not encoded in proper/desired format.

How does it appear when you view the source of the web page? There is a chance that the source is correct but it's just that the output is not what you wanted it to be.
May 11 '08 #2
pedalpete
110 100+
I'm not that sure, but I do have cases like that in another programming language. Turns out that my page is not encoded in proper/desired format.

How does it appear when you view the source of the web page? There is a chance that the source is correct but it's just that the output is not what you wanted it to be.
the source looks good (as I'm making it for testing purposes).

I have the encoding of the page set to utf-8, and when I do any sort of 'output', echo, etc I get the same value as the source, without slashes (even after trying to add slashes), so 'we're' comes out 'we're' , and not 'we\'re'.

Then of course when the goes into the database, it comes out 'we’re'.
May 11 '08 #3
Atli
5,058 Expert 4TB
Hi.

This does indeed appear to be a problem with your charsets.
Just try forcing your browser to render this page using the UTF-8 charset and view your first post... notice that the incorrect output will display as you initially wanted it to be.

Are you passing the text through any of PHP's functions before adding it to the database?
Like for example the htmlentites function?

If so, you may need to specify that you want it to use the UTF-8 charset or it may return it in the default (ISO-8859-1) charset.
May 12 '08 #4
pedalpete
110 100+
Hi Atli,

I am forcing the character set with this line right at the top of my page. is there another way to do it?
Expand|Select|Wrap|Line Numbers
  1. header("Content-type: text/html; charset=utf-8");
  2.  
I am passing the data through 'addslashes()' before adding it to the database (thinking that slashes are what helps it get into the db cleanly). I have also tried mysql_real_escape_string, but neither worked.
May 12 '08 #5
pedalpete
110 100+
So I am decoding this puzzle of why 'addslashes' doesn't seem to be working, and it seems their may be a problem with the way I have written my inset query.

I've always used quotes around the variables in the insert statement, but if I remove the quotes I get a mysql error where the slashes should be. If I have quotes in the mysql stament, the insert occurs, but the statement gets inserted with 'special characters' instead of slashes.

here's what I mean re: mysql
Expand|Select|Wrap|Line Numbers
  1. ...VALUES('$address', '$title')
  2. vs
  3. ...VALUES($address, $title)
  4.  
Does this clue anybody in to what my error is?

Now I can get a mysql error, but that doesn't solve why addslashes() isn't working in the first place.

more code
Expand|Select|Wrap|Line Numbers
  1.  
  2. $eventAddress = "$eventStreet, $eventCity, $eventState, $eventZip";
  3. $eventAddress = addslashes($eventAddress);
  4. // echo $eventAddress.'<br />';
  5.  
  6. list($date, $time) = explode(' ', $eventDateTime);
  7. list($month, $day, $year) = split('-', $date);
  8. $date = date('Y-m-d', mktime(0,0,0,$day,$month,$year));
  9. // echo $date.'<br />';
  10. // echo $time.'<br />';
  11.  
  12. $insertShow = "INSERT INTO shows ( address, date, time) VALUES ( '$eventAddress', '$date', '$time')";
  13.  mysql_query($insertShow)or die(mysql_error()); 
  14.  
  15.  
May 13 '08 #6
dlite922
1,584 Expert 1GB
So I am decoding this puzzle of why 'addslashes' doesn't seem to be working, and it seems their may be a problem with the way I have written my inset query.

I've always used quotes around the variables in the insert statement, but if I remove the quotes I get a mysql error where the slashes should be. If I have quotes in the mysql stament, the insert occurs, but the statement gets inserted with 'special characters' instead of slashes.

here's what I mean re: mysql
Expand|Select|Wrap|Line Numbers
  1. ...VALUES('$address', '$title')
  2. vs
  3. ...VALUES($address, $title)
  4.  
Does this clue anybody in to what my error is?

Now I can get a mysql error, but that doesn't solve why addslashes() isn't working in the first place.

more code
Expand|Select|Wrap|Line Numbers
  1.  
  2. $eventAddress = "$eventStreet, $eventCity, $eventState, $eventZip";
  3. $eventAddress = addslashes($eventAddress);
  4. // echo $eventAddress.'<br />';
  5.  
  6. list($date, $time) = explode(' ', $eventDateTime);
  7. list($month, $day, $year) = split('-', $date);
  8. $date = date('Y-m-d', mktime(0,0,0,$day,$month,$year));
  9. // echo $date.'<br />';
  10. // echo $time.'<br />';
  11.  
  12. $insertShow = "INSERT INTO shows ( address, date, time) VALUES ( '$eventAddress', '$date', '$time')";
  13.  mysql_query($insertShow)or die(mysql_error()); 
  14.  
  15.  
Can you add a die() statement just before mysql_query();

die($insertShow);

That should halt programming and exit and print the SQL given to MySQL. I just want to see what that looks like.

Thanks,

Oh and post the output of the page source here.


DM
May 13 '08 #7
Atli
5,058 Expert 4TB
You should always put strings inside single-quote marks in your SQL statements. Otherwise MySQL will try to parse them as columns or extra clauses or something along those lines.

You can use the <meta> tags to *set* the charset on you pages. Something like:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  4. </head>
  5. <!-- etc -->
  6. </html>
  7.  
Don't know if that will change anything tho. The header you showed us should do pretty much the same thing.
May 13 '08 #8
pedalpete
110 100+
thanks d-lite and Atli, you are definately correct

I went around and around in circles with character encoding until I came with what I think is the simple solution.

I can call htmlentities() and encode it all before putting it into the db. The stuff I'm dealing with is canadian, so i figure that is easier than dealing with all the accents on french characters with slashes etc.

I can't share the source file as it's a local file that I can't share.

Atli, I don't know how I missed your comment about setting forcing the character set in my browser, but now I can see more clearly. unfortunately, i still don't see how I can fix that.

Is there any reason I shouldn't store htmlentities in my database?
I'll just convert the string before I pass it into a query (which will mostly be in numeric columns and id's anyway).
May 13 '08 #9

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

Similar topics

1
by: leegold2 | last post by:
When I look directly in my db field I see a difference between these two functions. The top line (seebelow) was inserted with addslashes vs. the bottom line where I used mysql_real_escape_string....
0
by: Bob Bedford | last post by:
I've to put datas from user's input in a database. I've taken a function from internet (don't remember where) formatting most of the values: function GetSQLValueString($theValue, $theType,...
4
by: Jan Pieter Kunst | last post by:
Q. How do I use addslashes() and stripslashes() when dealing with HTML forms and database INSERTs, UPDATEs and SELECTs? A. It depends on the setting of the php.ini directive "magic_quotes_gpc"....
2
by: Marcus | last post by:
Hello, My php.ini file currently has magic quotes set to On, but I have read that it is better to code with it off. Currently with magic quotes on, I only use stripslashes() to properly...
2
by: Cruella DeVille | last post by:
I must have som errors in my understanding of strip- vs addslashes. I thought that if a user submitted eg a username, like this username=siv' drop database test; I should addslashes to escape ' and...
4
by: Areric | last post by:
hey all, I recently got in a bit of a fight with my webhost because he made some changes to my server. Specifically they updated php without telling me. They are now running PHP 4.4.1 (not sure...
5
by: lawrence k | last post by:
This seems so simple, I can't believe its tripping me up. I've a database class with a query method that looks like this: function query($query=false) { global $controller; // $query =...
6
by: redog6 | last post by:
Hi I have a webform with many free text fields and have a problem with apostrophes and single quotes as this breaks the mysql query string. I obviously need to escape these characters -...
13
by: ndlarsen | last post by:
Hello. It's been a while since I used php. Since then magic quotes has been deprecated and will be removed when php 6.0 hits. My question is, what should I be using when submitting data to a...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...
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.