tags:

views:

156

answers:

3

I know this is a very basic question, thats why I just want a simple answer please, there is several ways to my user input safe for mysql.

Is using this the BEST method

mysql_real_escape_string()

On all user submitted items going into a mysql query?

If I use the above, do I need to use another function on that date when I get it back from mysql to show on a PHP page?

+2  A: 

escape input, filter output.

  • You should use mysql_real_escape_string() or prepared statements going into the db.
  • You should use htmlentities() on any data generated by an end user when displaying on a page.

Please note that htmlentities will not handle every possible cross-site scripting attack depending on the user's browser and the particular attack vector they used. Many individuals use a sanitization library like HTML Purifier to cleanse their data prior to displaying it on a page.

cballou
I was just looking at htmlentities(), it is saying: A 'quote' is <b>bold</b> ====is turned into=== A 'quote' is <b>bold</b> If I run this and view source of page with it show up as html or like this?
jasondavis
@jasondavis, it will turn valid HTML tags into their entity (text) equivalents. If your output variables contain HTML you should look into utilizing HTML Purifier.
cballou
+2  A: 

PHP has a very good filter Function

http://php.net/manual/de/ref.filter.php

/*** use a callback filter to mysql_real_escape_string ***/
$answer = filter_input(INPUT_POST, "answer", FILTER_CALLBACK, array("options"=>"mysql_real_escape_string"));

/*** create an sql query ***/
$sql = "INSERT INTO quiz (answers) VALUES ('{$answer}')";

/*** echo the query ***/
echo $sql;
streetparade
+3  A: 

Using prepared statements is the best way to put data into MySQL. Prepared statements explicitly tells MySQL what is SQL and what is data, so MySQL won’t execute any SQL in the data.

You can get started with prepared statements with Mysqli.

As for showing the data in your PHP pages, you can use htmlspecialchars() to escape your output.

Chasen Le Hara
Prepared statement is the way to go. Clean code, smart, bug-free.
Clash