php mysql显示到页面上,如何通过php在mysql中插入特殊字符并显示在html页面上

how to insert special characters into a database(MySQL) like

Registered symbol ( ® ) OR

Copyright sign ( © ) OR

Trade Mark sign ( ™ )

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

What I have to do in both side (front end and back end), please elaborate

Which function is more effective?

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 am a bit confused what is the difference between

htmlentities() and htlspecialchars(), and which one i have to use?

which function should be used addslashes() or stripslashes() when insert into database?

解决方案

Just simply add those symbols to your text, and execute it as SQL query:

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

When you want to display it one the website don't do anything with these symbols (but remember to escape at least , & (using htmlspecialchars()) cause those has special meaning in XML/SGML (HTML) documents)

PS. Also remember to escape text passed to SQL query using mysql_real_escape_string() to avoid any SQL Injection problems. If your server has magic_quotes_gpc enabled disable it or at least filter your GET/POST/COOKIE data to its raw value. You should always consciously escape values.

EDIT:

According to your comment... I don't remember whether magic_quotes_gpc are enabled by default but you can easily undone magic quotes effect. Just on the very beginning of your PHP code add something like this:

if (get_magic_quotes_gpc()) {

array_walk_recursive($_GET, 'stripslashes');

array_walk_recursive($_POST, 'stripslashes');

array_walk_recursive($_COOKIE, 'stripslashes');

}

Now each GPC value should be always raw - without quotes - so you have to escape it manually before passing any variable into query.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值