$wpdb>更新或$wpdb>插入会导致在引号前面添加斜杠

2022-08-30 14:28:58

这个问题已经在不同的地方提出过几次,但我还没有找到一个明确而明确的答案。大多数解决方案涉及人们说要禁用php.ini文件上的魔术报价(我这样做了)或修改核心WP文件。

无论如何,问题是这样的:为什么每次我使用$wpdb->插入或$wpdb->更新时,在任何单引号之前都会添加斜杠。例如:

我吃过草莓变成我吃过草莓

以下是我使用的示例代码:

$id = $_POST['id'];
$title = $_POST['title'];
$message = $_POST['message'];

$wpdb->update('table_name', array('id'=>$id, 'title'=>$title, 'message'=>$message), array('id'=>$id))

同样的问题在这里:Wordpress数据库输出 - 删除SQL注入转义,但除了“禁用魔术引号”之外,它从未得到解决。


答案 1

在这上面花了一天的时间之后,答案如下:

Wordpress在$_POST声明中转义,而不是在实际的插入中,这很奇怪。

$id = stripslashes_deep($_POST['id']); //added stripslashes_deep which removes WP escaping.
$title = stripslashes_deep($_POST['title']);
$message = stripslashes_deep($_POST['message']);

$wpdb->update('table_name', array('id'=>$id, 'title'=>$title, 'message'=>$message), array('id'=>$id));

这样做将意味着WP不会在任何引号之前添加斜杠。


答案 2

更多的信息 - WordPress决定通过添加“魔术引号”让人们认为他们疯了,即使你从3.0版本开始就关闭了它。对 $_REQUEST、$_GET、$_POST、$_COOKIE 或 $_SERVER 的任何访问都将受到影响。看。wp-includes/load.php

 /* Add magic quotes to $_GET, $_POST, $_COOKIE, and $_SERVER.
 * @since 3.0.0
 */
function wp_magic_quotes() {
        // If already slashed, strip.
        if ( get_magic_quotes_gpc() ) {
                $_GET    = stripslashes_deep( $_GET    );
                $_POST   = stripslashes_deep( $_POST   );
                $_COOKIE = stripslashes_deep( $_COOKIE );
        }

        // Escape with wpdb.
        $_GET    = add_magic_quotes( $_GET    );
        $_POST   = add_magic_quotes( $_POST   );
        $_COOKIE = add_magic_quotes( $_COOKIE );
        $_SERVER = add_magic_quotes( $_SERVER );

        // Force REQUEST to be GET + POST.
        $_REQUEST = array_merge( $_GET, $_POST );
}

推荐