I'm not sure if this should go in the MySQL board, or this, since it deals with both PHP and MySQL.
But anyway, I am making a sports memorabilia site for a guy. On the item creation page, after I call the function to prevent sql injection, no matter what order I call it, and no matter what other functions I call, it always stores the product description incorrectly. For example, if I include new paragraphs or line breaks in the description text box, and then create the item, on the page the user sees that displays the item, it either shows a /r/n for each new line or line break, or it doesn't show them at all, and treats the entire product description as a single paragraph. On the page that inserts the item into the table, I have tried $productdesc = htmlentities($productdesc), I tried $productdesc = nl2br($productdesc) <- this is the one I used before I put in the code to prevent sql injection, and it worked, but I know I need to prevent sql injection, so I know that something else needs to be done, or done differently.
Before I started doing the sql injection function, it didn't do this, and I used the nl2br function, which seemed to store it in the mysql table just fine, and I would simply use the html_entity_decode function to take away the <br><br> and display a normal new paragraph.
This is the code I'm using to clean any possible sql out of the field:
function cleanQuery($string)
{
if(get_magic_quotes_gpc()) // prevents duplicate backslashes
{
$string = stripslashes($string);
}
if (phpversion() >= '4.3.0')
{
$string = mysql_real_escape_string($string);
}
else
{
$string = mysql_escape_string($string);
}
return $string;
}
What do you guys think? Is there a way to keep the html line breaks in there while preventing sql injection? I know there has to be some correct way of doing this. I hope I explained this adequately. If not, just ask and I'll try to explain it a different way.
Thanks.