Solved

dealing with apostrophes when inserting and reading from a postgreSQL database

Posted on 2006-07-12
8
387 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

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.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

770 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