Link to home
Start Free TrialLog in
Avatar of Yuri Boyz
Yuri BoyzFlag for Uganda

asked on

Single quote truncate the data

when a single quote is present in data in my variables i.e. $projects_row['project_title'], $projects_row['project_detail'] then it truncate my data. These variables are receiving data from Database. I have escaped my data with addslashes and mysqli_real_escape but still data is truncated. Any solutions?

$projects_name[] = "<div class='isotope'><li class='color-shape isotope_selector ". $filter_class2."'
					id=project-id-5a01bffbcfbeb40a008b458a data-id=5a01bffbcfbeb40a008b458a>
					<span data-caticons='" . $filter_icons_all."'  data-id='" . $projects_row['id']."' data-title='" . $projects_row['project_title']."' data-detail='" . addslashes($projects_row['project_detail'])."' data-mapimage='" . $projects_row['project_image']."' data-mapgallery='" . json_encode($project_gallery_images)."'  class='li-txt' style='color:#ff0000';>
					<a id='myAnc' href='#' data-location= '".$projects_row['coords']."'>".$projects_row['project_title']."</a></span>
					<span class='thumb-container'>".$filter_icons_all."</span>			 
					</li></div>";	
					
					
echo json_encode(array("project_title"=>$project_title,"project_detail"=>$project_detail));							

Open in new window

Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

See if the simple trick of reversing your quotes in the code works as seen here:
https://www.sitepoint.com/community/t/data-read-from-mysql-posted-into-textbox-truncates-at-apostrophe/101928
Just convert the single apostrophes to the HTML entity &#39; when you use the value inside of a quoted HTML element value:

<blahblah value='It's me, Mario!'> <-- Wrong
<blahblah value='It&#39;s me, Mario!'> <-- Right

The browser will then treat the &#39; as an apostrophe, the way you want it to behave.  You can use htmlentities() to do this automatically for you:

$x = "It's me, Mario!";
echo "<blahblah value='" . htmlentities($x, ENT_QUOTES) . "'>";

http://php.net/htmlentities

The addslashes() is not necessary - that's escaping for languages like Javascript or PHP or MySQL queries. In HTML (and XML, for that matter), you don't escape with slashes - you convert special characters to entities. That's just how the browser reads things.
Avatar of Yuri Boyz

ASKER

Yes I already did this and it works. But in my webpage there is pagination of php which also passes data with javascript. When I use NExt page and Previous page then it shows me text like It&#39;s. If I click  on specific record then it works fine. So any solution for this?
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.