[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 361
  • Last Modified:

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












0
hew1
Asked:
hew1
  • 4
  • 2
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now