纠正PHP方法在MySQL DB中存储特殊字符

Ant*_*cia 6 php mysql sql-injection html-encode html-entities

使用PHP,在MSQUL数据库中存储特殊字符(如下所示)的最佳方法是什么,以避免注入.

« " ' é à ù
Run Code Online (Sandbox Code Playgroud)

这是我现在这样做的方式:

$book_text=$_POST['book_text'];
$book_text=htmlentities($book_text, "ENT_QUOTES");
$query=//DB query to insert the text
Run Code Online (Sandbox Code Playgroud)

然后:

$query=//DB query to select the text
$fetch=//The fetch of $book_text
$book_text=html_entity_decode($book_text);
Run Code Online (Sandbox Code Playgroud)

这样,我的所有文本都以HTML实体格式化.但我认为这占用了大量的数据库空间.那么,有更好的方法吗?

小智 6

使用utf8编码来存储这些值.

避免注射使用mysql_real_escape_string()(或准备好的陈述).

防止XSS使用htmlspecialchars.