Solved

dealing with apostrophes when inserting and reading from a postgreSQL database

Posted on 2006-07-12
8
396 Views
Last Modified: 2013-12-12
Hi,

I am using this code:

echo "<input type='text' name='passwordquestion' value='".htmlspecialchars(stripslashes(trim($row['passwordquestion'])))."' size='50'>";

when displaying a field from a database which may include an apostrophe within the text.  If the text is "Mother's maiden name" then it is only showing the word Mother in the box displayed on screen, but if you view the source of the page, the value of the field is the full text.

When I am adding the value into the database, I am using this code around the data:

$mypwdQn = $_POST['passwordquestion'];
$mypwdQn = addslashes($mypwdQn);

The data is added fine into the database.

Can someone please help me to get the data to display correctly in the input box?

Thanks heaps,
Heather
0
Comment
Question by:hmaloney
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 5

Expert Comment

by:koolie
ID: 17096119
That's the stripslashes function

<?php
$str = "Is your name O\'reilly?";

// Outputs: Is your name O'reilly?
echo stripslashes($str);
?>
0
 

Author Comment

by:hmaloney
ID: 17096773
Thanks koolie, I'm using that functionality already as per my code, but it is not working.
0
 
LVL 5

Expert Comment

by:koolie
ID: 17099251
My mistake...What you could try doing is stripping the slashes before the data is inserted into the db rather than running all of the function commands from the input box.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Assisted Solution

by:ner0187
ner0187 earned 45 total points
ID: 17100336
Try getting rid of the htmlspecialchars() function when you're pulling it back from the database.

If you want to use this function, you should really execute it before inserting to the db.

Not sure if this will help, but it would be a better way of doing it, e.g. if you wanted to use the data somewhere else, to save on code etc.

All the best
0
 
LVL 27

Accepted Solution

by:
yodercm earned 45 total points
ID: 17101080
You should use htmlspecialchars( ), or better yet htmlentities( ) because it handles quotes better, on ALL input before it goes into the database.  This function replaces all special characters with their &nn html code equivalents, so be sure to allow extra room in the field lengths in the database for it (if they are not TEXT fields).

http://us2.php.net/manual/en/function.htmlentities.php

http://www.ascii.cl/htmlcodes.htm

Once you have done this, you can retrieve the info from the database and display it directly without any further manipulation (as ner0187 said).
0
 

Author Comment

by:hmaloney
ID: 17103848
Okay.....

I am now saving the text to the database field using the htmlentities( ) function, and it is putting a \ in front of the apostrophe.

Then, when I display the contents of that database field in a webpage it all turns out nicely.  However, when I display the text inside the value attribute of an input field, the text stops just before the apostrophe.  I tried it with and without htmlentities() around the value.

I'd appreciate your further help!

Thank you.
0
 
LVL 27

Expert Comment

by:yodercm
ID: 17104423
Where is the backslash coming from?  htmlentities doesn't put it in.
0
 

Author Comment

by:hmaloney
ID: 17104826
ah!  I just discovered that the code I am using to do the SQL update is putting the apostrophe in itself....

hmmmmmm

so, if I have this text in my database "xyz\'xyz" and I want it to appear inside a value attribute like this:
<input type='text' name='myfield' value='xyz'xyz' > 

I've just worked out a way around this.... instead of putting all of the above in a echo statement, I am breaking out of the PHP code for all the HTML text, and just using PHP to print the value in between " "

Thanks for all your help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Ajax and PHP 4 47
Use Select Query to Return Results as a Form 9 43
Wordpress and Wufoo 1 39
syntax error, unexpected '?' in phpunit 5 28
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

733 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