tags:

views:

281

answers:

2

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?

A: 

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;}');
Samir Talwar
the OP's escaping was fine
Paul Dixon
+4  A: 

(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);
Paul Dixon
Thanks for the heads up but it is a inside a php mysql statement so $user_id is valid. :)
Drew
Still, show us the actual code. You're making an assumption.
Paul Dixon
full code added. I use $user_id in the four statements before that and they insert fine. thanks for your time
Drew
echo the $sql1 value and add that to your post, and run it by hand in a mysql client to see what error you get.
Paul Dixon
it runs fine if i put it straight into phpmyadmin. $user_id comes through fine with the other statements
Drew
It's the semi-colons causing the problems. Trying to find out how to escape them as \ doesn't seem to work.
Drew
Depending on the software and the database driver, you might not be able to run multiple statements. Run each one separately.
Paul Dixon
at the moment I am only running the one statement that is causing me a headache. it's the damn semi-colons! why wordpress thought it was a good idea to put those in i do not know.
Drew
nailed it, see my edit
Paul Dixon
you sir, are a genius! i owe you a drink! cheers for your time and the help.
Drew
No worries :)
Paul Dixon