Wordpress: wpdb->insert VERSUS wpdb->prepare(wpdb->query("INSERT

不问归期 提交于 2019-12-23 12:27:09

问题


I am wondering if wordpress' insert function also adds slashes to data. If it doesn't it would seem that the prepare query method would be better to prevent against SQL injection. I tried looking the issue up in there codex/api; however, it seems undocumented. Thanks!


回答1:


Wordpress uses ezSQL to query the database. Technically, it is not an abstraction layer but it does take away some of the boilerplate code. ezSQL has a function escape so I assume that Wordpress would always call the escape function before executing a query. But to be certain you would have to take a look at the source code.

This is how you escape a string in Wordpress:
$safe_string = $wpdb->escape($unsafe_string);




回答2:


This question is a little old and the codex may have been updated since it was asked. Both wpdb->insert() and wpdb->prepare() provide the same level of safety regarding SQL escaping of input data.

The codex states that the both the column and data values provided to the insert method should be raw, not SQL escaped.

I also took a quick look at the source to confirm. The implementation for the insert method uses wpdb->prepare().



来源:https://stackoverflow.com/questions/2389064/wordpress-wpdb-insert-versus-wpdb-preparewpdb-queryinsert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!