2

First time with this trouble when dealing with a MySQL table.

I'm inserting bar names in a table. If the bar is called "Tim's Bar" and I insert it straight away I get an error and the data is not inserted.

How do you instert properly the ' in the table?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user712027
  • 572
  • 6
  • 9
  • 22
  • 1
    @user712027, it sounds like you have a SQL-injection problem. See: http://stackoverflow.com/questions/332365/xkcd-sql-injection-please-explain – Johan Apr 28 '11 at 09:18

5 Answers5

7

Use mysql_real_escape_string():

http://php.net/manual/en/function.mysql-real-escape-string.php

magma
  • 8,432
  • 1
  • 35
  • 33
5

Use PDO with prepared statements.

$query = $pdo->prepare('INSERT INTO bars (name) VALUES (?)');
$query->execute("Tim's Bar");

It's superior (and safer) than using the mysql(i)_* family of functions directly.

Sander Marechal
  • 22,978
  • 13
  • 65
  • 96
0
INSERT INTO your_table SET person_name = 'Tim\'s Bar';

Note the \'

Michael J.V.
  • 5,499
  • 1
  • 20
  • 16
0

I believe you should insert it as 'Tim\'s Bar'.

Regards

Masiar
  • 20,450
  • 31
  • 97
  • 140
0

addslashes() by the insert, and stripslashes() by the output would also work

Flask
  • 4,966
  • 1
  • 20
  • 39
  • This is outright incorrect, on both counts. First, `addslashes` is *not* the way to escape data before inserting it into the database. Second, calling `stripslashes` on data going from the database to the screen is not only unnecessary, doing so will corrupt your data. – user229044 Apr 28 '11 at 17:25