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,438
  • 68
  • 55
  • 126
user712027
  • 562
  • 5
  • 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,262
  • 1
  • 34
  • 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,658
  • 12
  • 62
  • 94
0
INSERT INTO your_table SET person_name = 'Tim\'s Bar';

Note the \'

Michael J.V.
  • 5,391
  • 1
  • 19
  • 16
0

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

Regards

Masiar
  • 18,510
  • 29
  • 92
  • 135
0

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

Flask
  • 4,916
  • 1
  • 19
  • 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