I am a newbie and I am working with PHP 4.1.2/Mysql/Apache on MacOSX.
If a text has special characters such as an apostrophe i get an error message as soon as I want to write the text via a form into my database. I now found out that one can escape these problems by using the addslashes or the mysql_escape_string functions. I tried it, but I was not succesful and I do not exactly know where to put the function into my code. It definetly does not work this way:
$query = "INSERT INTO news_01(Headline, copy, Author, Publication, Releasedate) VALUES(mysql_escape_string('$Headline', '$copy', '$Author', '$Publication', '$Releasedate')";
Can anybody help?
How to get rid of special character/mysql_escape_string???
Moderator: General Moderators
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Try something like,
Mac
Code: Select all
$query = "INSERT INTO news_01(Headline, copy, Author, Publication, Releasedate) VALUES('".addslashes($Headline)."', '".addslashes($copy)."', '".addslashes($Author)."', '".addslashes($Publication)."', '".addslashes($Releasedate)."')";twigletmac is correct, but I prefer this as it is easier to read and spot syntax errors
And when you retrieve the values from your database you can use StripSlashes() function.
Code: Select all
$Headline = AddSlashes($Headline);
$copy = AddSlashes($copy);
$Author = AddSlashes($Author);
$Publication = AddSlashes($Publication);
$ReleaseDate = AddSlashes($ReleaseDate);
$Query = "INSERT INTO news_01(Headline, copy, Author, Publication, ReleaseDate) VALUES ('$Headline','$copy','$Author','$Publication','$ReleaseDate')";- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Happy to help but the best place to go when you want to know about a particular function is the manual:
Basically they just escape different things.
You need to use stripslashes() when you retrieve the data to display on screen because you probably don't want to display all the escape characters.
Mac
You need to use stripslashes() when you retrieve the data to display on screen because you probably don't want to display all the escape characters.
Code: Select all
<?php
$output = addslashes("He's over there");
echo $output; // Displays -- He''s over there
$output = stripslashes($output);
echo $output; // Displays -- He's over there
?>