single quotes & double quotes insert into mysql
Hi
I have a php news like script where I can submit news etc. Now in textarea if I have a news that has "kjlk" double quotes like that then it gives out an error when you submit it to be saved in database. How can I make the double quotes or single quotes or any other character that is illegal to be inserted in mysql db to be saved in the mysql database without any error.
Thanks alot and please do show me with an example. I am lil newbie at all this.
This is a million dollar hint about the double qoutes issue
I laboured attempting to insert data from one mysql database to the other...after executing numerous php- mysql codes, i kept running into the problem associated with "double qoute" errors. I then used the stripslash() addslash() html entitities("" enqoute), str_replace() etc. But all in vain the above solutions could only solve one or the other...the errors where either from "html based content" or "double quoted text content", and in other cases the mysql engine was executing key words such as 'from' 'and' 'as' from my data content instead of storing the data.
After almost six hours of head aching and numerous attempts, a search with "php mysql insert into double qoutes problem" on google brought me here to dynamic drive. With just this simple hint; "mysql_real_escape_string()" my job was done in 2 minutes. Thanks "djr33" for this simplistic yet a very million dollar hint.
here is the simple but effective php script that I used on www.acholinet.com. It might help some other amatuer like me save time in copying data from one mysql table to the other without running into these so called 'double quotes' etc problems
<?
//inlcuded code to connect to mysql database
include ("connecttodatabse.php");
//sql query section
//got data from mysql table text_content to insert in mysql table data_content
//notice the detail mysql error catch..after all my fustrations
$count = 0;
$q1 = "select * from text_content";
$r1 = mysql_query($q1)or die("<b>A MySQL error occured</b> Query: " . $q1 . " <br /> Error: (" . mysql_errno() . ") " . mysql_error());
while($a1 = mysql_fetch_array($r1))
{
$count++;
$title = mysql_real_escape_string($a1[title]);
$catid = mysql_real_escape_string($a1[catid]);
$introtext = mysql_real_escape_string($a1[introtext]);
$full_text = mysql_real_escape_string($a1[full_text]);
//inserted data into the columns of the new table called 'display'
$q2 = "insert ignore into display set news_subject = \"$title\",
news_cat = \"$catid\",
news_news = \"$introtext\",
news_extended = \"$full_text\" ";
$f1 = mysql_query($q2) or die("<b>A MySQL error occured at content: $count</b> Query: " . $q2 . " <br /> Error:
(" . mysql_errno() . ") " . mysql_error());
}
echo " Number of records copied is: $count";