1

I'm having problems getting special characters like apostrophes and such from being added into my database.

I have the following code that adds data from a form into my database.

mysql_query("INSERT INTO people(`ID`, `Name`, `Description`)
 VALUES (NULL, '$name', '$desc')") or die(mysql_error());

Form code looks like so:

$query = "SELECT * FROM people";
$result = mysql_query($query);
while ($person = mysql_fetch_array($result)){
echo "<h3>" . $person['Name'] . "</h3>";
echo "<p>" .$person['Description'] . "</p>"}`

How would I go about fixing this so that the string field accepts special characters?

Jason Chen
  • 2,487
  • 5
  • 25
  • 44
  • 2
    try using `mysql_real_escape_string()` function – sumitb.mdi Mar 13 '14 at 06:27
  • 1
    Update from the [deprecated/legacy `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) and [prevent SQL injection by using parameterized queries](http://stackoverflow.com/a/60496/2864740) (suggestions to use mysql_real_escape_string are a hack for code that *should* be updated anyway) – user2864740 Mar 13 '14 at 06:33

3 Answers3

3

if you want add special character in MySQL database use

mysql_real_escape_string($name)

after that insert into database

مسعود
  • 679
  • 10
  • 25
2

You could use mysql_real_escape_string

$insert_data = mysql_real_escape_string($input_data);

Assuming that you have the data stored as $input_data

BKM
  • 6,949
  • 7
  • 30
  • 45
0

Just use mysql_real_escape_string

 $insert_data = mysql_real_escape_string($input_data);

Assuming that you have the data stored as $input_data

daR
  • 250
  • 2
  • 19