Wordpress dbDelta function fails to execute my que

2019-09-16 14:00发布

问题:

I have tried escaping all of the characters and that doesn't work. I need to insert this string specifically. Can anyone tell me what I'm missing? The other 4 insert statements work fine. Below is the full code:

$user_id = mysql_insert_id();


$sql1 = "INSERT INTO `wp_usermeta` VALUES (NULL, $user_id, 'nickname', '$email');
INSERT INTO `wp_usermeta` VALUES (NULL, $user_id, 'rich_editing', 'true');
INSERT INTO `wp_usermeta` VALUES (NULL, $user_id, 'comment_shortcuts', 'false');
INSERT INTO `wp_usermeta` VALUES (NULL, $user_id, 'admin_color', 'fresh');";

$sql1 .= "INSERT INTO `wp_usermeta` VALUES (NULL, $user_id, 'wp_capabilities', 'a:1:{s:10:\"subscriber\";b:1;}');";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql1);

Thanks

UPDATE

The problem lies with the semi-colons in the string. I have tried escaping these in the normal fashion () but that doesn't seem to work here. The string now looks like a:1:{s:10:\"subscriber\"\;b:1\;} Any ideas?

回答1:

(This answer evolved as the OP provided more code. This is the final, simplified answer!)

The wordpress dbDelta function explodes the string it is given on ';', which is inside your JSON. What you need to do is separate your queries into an array, and pass that to dbDelta instead.

$sql1=array();
$sql1[]="insert into....";
$sql1[]="insert into....";

dbDelta($sql1);


回答2:

You most likely need to escape the \ characters with another \ - this is necessary because \' will let you insert a single quote.

INSERT INTO `wp_usermeta`
VALUES (NULL, $user_id, 'wp_capabilities', 'a:1:{s:10:\\"subscriber\\";b:1;}');


标签: sql wordpress