Why does htmlspecialchars not work?

Here's my code:

				echo "yes";
				$description= htmlspecialchars("$_POST[description_html]", ENT_QUOTES);
				echo $description;
			$sql_1="select id from calendar order by id DESC LIMIT 1";
			$event_id= $row_1['id']+1;
			$short_name = $mysqli->real_escape_string(trim($_POST['short_name']));
			$the_region = $mysqli->real_escape_string(trim($_POST['region']));
			$article_date = $_POST['sdate'];

			$sql="insert into calendar (short_name, description, state, region, date, event_id, market, html_content) values ('$short_name', '$description', '$the_state', '$the_region', '$article_date', '$event_id', '$the_market', '$html_content')";
			echo $sql;

Open in new window

Here's what I get:

Notice the "yes" in the upper left hand corner. That means my code is "sensing" the fact that I've got incoming HTML code correctly. Not I'm going to insert it into the database using "$description= htmlspecialchars("$_POST[description_html]", ENT_QUOTES);." But when you look at the printed $sql statement, you've got:

insert into calendar (short_name, description, state, region, date, event_id, market, html_content) values ('more web', '', 'South Carolina', 'CATN', '2015-10-15', '5319', 'Charleston - CHS', 'Y')

It's like the $_POST['description_HTML'] has disappeared. Yet, it prints just fine when I simply "echo" later in the page.

What's wrong with my insert statement?
Bruce GustPHP DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
It's not just the INSERT statement  that is wrong -- it's the whole strategy.  Let's try to deconstruct the concepts here and show you some ways of finding the data.

After a POST-method request is made to your PHP script, you can see the POST request data.  You use var_dump($_POST) to do this.  Then you use your browser's "view source" capability to see what is really there.  You cannot trust the browser output - you must use "view source" for this.

When you receive data from a POST-method request and you want to store it in your database, you take two basic steps.  First, you sanitize the data, checking to see that numeric fields have numbers, date fields have dates, etc.  Once all of the data passes the sanitize filters, then you use the context-appropriate real_escape_string() functions to prepare the data for safe use in a query.  And then you insert it into the database.

When you retrieve the data from the database and want to display it to your client's browser, you use htmlentities() or similar to prepare the data for safe display in the browser.

That's the overall strategy - filter input, escape output.  Now on to the specifics, which may involve two things that I think we've talked about before: error_reporting() and the correct use of quotes.

Always, always, always use error_reporting(E_ALL); no exceptions.

Correct use of quotes is described here.

This looks wrong to me and I think it should have been flagged by error_reporting(E_ALL):
$description= htmlspecialchars("$_POST[description_html]", ENT_QUOTES);

Open in new window

Maybe try it this way instead:
$description = $mysqli->real_escape_string($_POST['description_html']);

Open in new window

Of course, there is also the possibility that $_POST did not contain any information in the description_html element.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bruce GustPHP DeveloperAuthor Commented:

Project Completed!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.