To insert an apostrophe into the database using SQL you need to put two apostrophes in the text where you want just one. For example, to insert the phrase "what's it?" into a database, the SQL code looks like:
INSERT INTO mytable (phrases) VALUES ('what''s it?')
In PHP there is a string function which allows you to replace on the variable easily: str_replace This function replaces one value with another in a string. So before you insert data in the database you should replace all single apostrophes with double-apostrophes. For the example variable, the PHP code is:
$comment = str_replace("'","''",$comment);
9 comments:
This is a great tip. This is the most effective method i have found. Simple.
Glad your site came up first in the search, it was going nuts trying to figure this out. Very quick and simple solution.
Thanks a lot. Great solution. Ales Loziak (CZ).
THANKS !!!
Absolutely works perfect!
Thanks a lot!
Many thanks for that info. It was just what I needed, straight to the point and it works. Regards - John
You are awesome.....
addslashes is another php function which does the same job. Pretty neat too. http://php.net/manual/en/function.addslashes.php
Post a Comment