7

I want to run the following query with a single quoted value.

INSERT INTO web_camp_keywords (web_id, keyword) VALUES (195, 'abc'hotels')

I just want to add abc'hotels value. I used backslash, but it did not work.

INSERT INTO web_camp_keywords (web_id, keyword) VALUES (195, 'abc\'hotels')

How can I resolve this?

3
  • 1
    try INSERT INTO web_camp_keywords (web_id, keyword) VALUES (195, 'abc''hotels') . but why not prepared statement? Commented Aug 8, 2012 at 5:20
  • tnx. I have not experence about prepared statement. I'll try that. Commented Aug 8, 2012 at 5:33
  • 2
    @Asurya then your application is FULL of what are called SQL injection security holes. Imagine what happens if the user who entered abc'hotels into a form instead entered abc'; DELETE FROM web_camp_keywords;-- or worse, abc';DROP SCHEMA public CASCADE;-- ? splat, there goes your entire database. See en.wikipedia.org/wiki/SQL_injection (Note: The DROP SCHEMA trick shouldn't work if your app connects with only the minimum required permissions - it shouldn't own the tables or be a superuser - but your app is probably the owner of the tables.) Commented Aug 8, 2012 at 6:36

2 Answers 2

8

You can escape the single quote with another single.

INSERT INTO web_camp_keywords (web_id, keyword) 
VALUES (195, 'abc''hotels')

But personally I think you should be using prepared statements with bind parameters.

Among other things, use of prepared statements with bind parameters is one of the easiest ways to help protect against SQL injection, the biggest source of security holes in web applications.

Sign up to request clarification or add additional context in comments.

Comments

2

Like Chris Moutray and others mentioned, it would be best if you used pdo and prepared statements. Here is an example on how you could prepare a statement, provide the statement with values and then execute it. I left out the connection.

$statement = $pdo->prepare("insert into web_camp_keywords (web_id, keyword) values (:id, :keyword)");
$statement->bindValue(':id', 195);
$statement->bindValue(':keyword', "abc'hotels");
$statement->execute();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.