我刚修复了一些代码的输出,使用 wpdb
函数从数据库中存储和检索的字符串中删除了斜杠。然后我希望必须在我的代码的另一部分修复相同的问题,其中字符串存储在数据库中并从数据库中检索,但我注意到没有添加斜线,所以我对何时自动添加斜线感到有点困惑。以下是我对这两个案例的观察:
案例 1 - 我需要去除输出中的斜杠
$_POST
数据自动用斜线转义$wpdb->insert
和 $wpdb->update
数据在数据库中以斜线结尾$wpdb->get_row
和 $wpdb->get_results
检索的数据有斜杠案例 2 - 我不需要去除输出中的斜线
$_POST
数据自动用斜线转义$wpdb->query
插入和更新数据,最终在数据库中没有斜杠$wpdb->get_row
和 $wpdb->get_results
检索的数据没有斜线我的问题是:
wpdb
函数时处理转义的最佳做法是什么?最佳答案
来自法典:
Both $data columns and $data values should be "raw" (neither should be SQL escaped). This means that if you are using GET or POST data you may need to use striplashes() to avoid slashes ending up in the database.
您可能还想看看这个部分:http://codex.wordpress.org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks
https://stackoverflow.com/questions/17690265/