Add ' to textarea

Hi

I've got a quesiton a bit like Q_21585750.html.  I've got a website where entries from textareas are saved to a MySQL database, and can later be viewed and edited.

I've got some code to reduce the risk of errors/malicous entries.   I use the code in a function applied to all text entered in the website (that may come from textareas or text boxes). Also use it to check any text variables picked up using $_GET.

  $value=trim($value);
  $pattern = "/[^a-zA-Z0-9\.\!\-\_\?\@\\r\,\ \)\(\']/";
  $value=preg_replace($pattern, "", $value);

My first problem was that the MySQL database generated errors when strings contained the ' character.  Can resolve that by adding the following rows to the function:

  $pattern2 = "/[']/";
  $value=preg_replace($pattern2, "\'", $value);

However, when I reload the information in a textarea to edit it, it gets cut short at the point where the ' appears.  Just wondering if there's a good way to get round this problem?

cheers

Hew












hew1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BogoJokerCommented:
Hi hew1,

There is an easier way! A few functions work, the most significant being:
mysql_real_escape_string:
http://www.php.net/manual/en/function.mysql-real-escape-string.php

Joe P
0
BogoJokerCommented:
Hmm, I may have read it a little wrong.  I read you putting information into the database, instead of the nice $pattern2 that you have, you could use the mysql_real_escape_string() putting the information into the mysql table.
Example usage:
$str = "Joe's String has a single qoute!";
$sql = "INSERT INTO table VALUES('mysql_real_escape_string($str)', 'Joe P')";

But it looks like your doing the opposite, going from a database into a textfield or likewise.  Well, I would like to see how your doing that, because it shold work if you have a textarea/textfield like so:
<textarea><?php echo $valueFromTheMysqlTable ?></textarea>
Likewise for a textfield:
<input type="text" value="<?php echo $valueFromTheMysqlTable ?>>

Joe P
0
hew1Author Commented:
Thanks!  Have added following code to function that cleans up text before it is saved.
 
 $value=mysql_real_escape_string($value);

I've got two text areas where the text is displayed after being saved, a "veiw" form and an "edit" form.  The view form now works fine (ie displays ' ).  Code below:

  <td width='65%' rowspan='3' class="td1"><textarea name="dispusercomment" cols="<?php echo  $cols?  
  >" rows="<?php echo $userrows ?>" readonly>Service Report: <?php echo $usercomment ?></textarea></td>

However, for some reason the edit form still cuts the text off at the first ' .  Code below:

  <td class="td2" <?php if($r==1 and $usercomment==NULL) echo " bgcolor='#FFFF99'"?>>
  <textarea name="usercomment" cols="70" rows="10"><?php echo $usercomment?></textarea></td>

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

BogoJokerCommented:
For safer code, both should be:
<?php echo $usercomment; ?>
semicolons are important in php, not needed (but perferred) in javascript.

Try that, tell me if you get the same results,
Joe P
0
hew1Author Commented:
Thanks, have made change.  Noticed another possible problem I hadn't mentioned while I was doing that.
$usercomment is passed from the view form to the edit form using the following line:

<input type='hidden' name='usercomment' value ='<?php echo $usercomment; ?>'>

may need to be?

<input type='hidden' name='usercomment' value ="<?php echo $usercomment; ?>">
0
BogoJokerCommented:
That would be the best way to do it, notice then you need to watch for double quotes now too.
Try something like:
<input type='hidden' name='usercomment' value ="<?php echo mysql_real_escape_string($usercomment); ?>">
or
<input type='hidden' name='usercomment' value ='<?php echo addslashes($usercomment); ?>'>

Double quotes or single quotes won't really matter once you escape all the quotes.  I would suggest trying the top one first, then addslashes if that doesnt work, add slashes escapes everything that could cause a problem.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.