Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing Invalid Characters in mySQL Table Using PHP

Status
Not open for further replies.

Wickersty

Programmer
Joined
Nov 13, 2002
Messages
51
Location
US
Hi,

Let's say I've got a mySQL Table with 5 text-type fields. A user fills out a standard HTML form, entering the text data s/he wants to store in these mySQL fields.

When I process the form using PHP, I want to make sure that the data does not contain any characters that would screw me up... characters like ' and " and ; etc.

Any advice would be greatly appreciated.

Thanks,

Jeff
 
or htmlspecialchars($string, ENT_QUOTE);
 
Wickersty,

Both answers above address the problem by encoding the offending characters. They do not remove or check for the existence, they just convert them.

If you want to exclude those characters from the text fields you should use a regular expression that removes checks for them.

It is not entirely clear from your question if converting the characters is what you want, or if it is removal.
 
I could just remove them, like the above mentioned suggestions would do. I'd like to convert them, though. Is it possible to convert, say a ' to a "safe" '

???

Thanks for the replies, all...

Jeff
 
Yeah, there're a couple possibilities here, and I'm not sure which you're getting at.

You need to make the string mysql safe to get it into the database, then you'll need to make it HTML safe to display it on the web page. Fortunately for you, all the necessary functions are listed above :)

Good luck,
Rob
 
Thanks all. I'll try your suggestions tonight and let you know how it works out. I appreciate the guidance.

Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top