Solved

Insert comment problem

Posted on 2007-04-02
3
178 Views
Last Modified: 2013-12-13
Experts.

I'm having some problems with inserting comment using form with word that contains a   '   , example when trying to insert word like simon's, it gives error.

$Query = "INSERT into classifieds (comment) VALUES( '$comment')";
$result=mysql_query($Query);

I tried to use the solution $comment = str_replace("'", "", "$comment"); it works but simon's will becomes simons. Is there a way to insert word with ' without getting an error and can be display on page?
0
Comment
Question by:keiluko
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 18839498
a minor change, and it will work as requested:
$comment = str_replace("'", "''", "$comment");
$Query = "INSERT into classifieds (comment) VALUES( '$comment')";
$result=mysql_query($Query);

0
 
LVL 24

Expert Comment

by:glcummins
ID: 18839514
Is there a reason you chose not to use addslashes()?

$Query = "INSERT into classifieds (comment) VALUES( '" , addslashes($comment) . "')";

This will preserve your data as-is, and still allow it to be inserted without error.
0
 

Author Comment

by:keiluko
ID: 18839546
glcummins: not sure about addslashes, been using the insert command without addslashes. but thanks I will use it in future project.

Thanks all.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question