Advertisement

Query and display text containing apostrophe Topic is solved

For Joomla! 5.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
pcsadmin
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Mon May 13, 2024 7:38 pm

Query and display text containing apostrophe

Post by pcsadmin » Mon Oct 14, 2024 11:50 pm

I have a form that includes a comment field. If a user types in a comment that contains an apostrophe, $db->quote works perfectly and saves the entire comment via a Mysql INSERT. However when I SELECT the field later to display as part of an Edit form, I only get the text prior to the apostrophe displayed in my form. What is the correct method to query and display the complete text?

Query to Select the data:
SELECT score, comment FROM #__scores WHERE .... (will only return 1 row)

Query results are accessed via :
$results = $db-loadRow();
$score = $results[0];
$comment = $results[1];

Results are displayed via:
echo "<tr><td colspan=5><input type='text' size=130 name='user_comment' value='".$comment."'></td></tr>";

Advertisement
pcsadmin
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Mon May 13, 2024 7:38 pm

Re: Query and display text containing apostrophe

Post by pcsadmin » Mon Oct 14, 2024 11:55 pm

I just saw the typo in my posted code that isn't in my actual program.

$results = $db->loadRow();

My program does have the greater than sign.

SharkyKZ
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 3079
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Query and display text containing apostrophe

Post by SharkyKZ » Tue Oct 15, 2024 5:31 am

You must escape any and all user input. When displaying user content on HTML pages, use htmlspecialchars() https://www.php.net/manual/en/function. ... lchars.php. Inside views and layouts you can use escape() method.
In database queries use parameters https://manual.joomla.org/docs/general- ... nsert-data.

pcsadmin
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Mon May 13, 2024 7:38 pm

Re: Query and display text containing apostrophe

Post by pcsadmin » Tue Oct 15, 2024 8:44 am

Thanks to SharkyKZ for the awesome answer. htmlspecialchars() fixed my issue and everything is working great now.

Advertisement

Post Reply

Return to “Joomla! 5.x Coding”