How to insert special character in mysql via php and display on html page

how to insert special characters into a database (MySQL), for example

Registered symbol ( ® )OR   Copyright sign ( © )OR  Trade Mark sign ( ™ )

Also I want to show as the original on the html page.

What do I need to do on both sides (front and back), please specify

Which function is more efficient?

Method 1:

$_GET = array_map('trim', $_GET);
$_POST = array_map('trim', $_POST);

if(get_magic_quotes_gpc()){
  $_GET = array_map('stripslashes', $_GET);
  $_POST = array_map('stripslashes', $_POST);  

  $_GET = array_map('strip_tags', $_GET);
  $_POST = array_map('strip_tags', $_POST);  
 }
 else{
  $_GET = array_map('mysql_real_escape_string', $_GET);
  $_POST = array_map('mysql_real_escape_string', $_POST);   
 }

Method 2:

  foreach ($_POST as $key=>$value){
        if (!get_magic_quotes_gpc()) {
          return addslashes(htmlentities(strip_tags($value),ENT_QUOTES,'UTF-8'));
          } 
          else {
             return htmlentities(strip_tags($value),ENT_QUOTES,'UTF-8');
          }
  }

I'm a little confused what's the difference between

htmlentities()and htlspecialchars(), and which one should I use?

which function should I use addslashes()or stripslashes()when pasting into the database?

+3
source share
3 answers

Just add these characters to your text and execute it as an SQL query:

INSERT INTO tbl_name VALUES ("Here my text: ©®");

, - ( , , <, >, & ( htmlspecialchars()), XML/SGML (HTML))

PS. , SQL-, mysql_real_escape_string(), SQL-. magic_quotes_gpc enabled, , , GET/POST/COOKIE . .

EDIT:

... , magic_quotes_gpc , . PHP- - :

if (get_magic_quotes_gpc()) {
  array_walk_recursive($_GET, 'stripslashes');
  array_walk_recursive($_POST, 'stripslashes');
  array_walk_recursive($_COOKIE, 'stripslashes');
}

GPC - - , - .

+4

PHP htmlentities():

htmlspecialchars() , htmlentities(), HTML .

: raw htmlentities(), HTML.

: .

+1

mysql_real_escape_string . , , , , .

htmlentities htmlspecialchars , htmlentities , , htmlspecialchars <, > , &, "

+1

Source: https://habr.com/ru/post/1729676/


All Articles