我想知道是否有人可以帮忙解决这个问题。我试图做一个php查询来更新Wordpress上的post meta值。 Pb:字符串必须非常长并且充满非正统字符,如双引号。
结果有点奇怪,因为它得到了更新但是在值周围添加了另一个序列,打破了它,如下所示:
S:5086:"的一个:28:{I:0;一个:7:.....
这是最初的查询,如果有人能够弄清楚如何使其符合规定并且令人惊讶:
update_post_meta( $product_id, '_wc_booking_pricing', 'a:28:{i:0;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:5:"0.027";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"1";s:2:"to";s:1:"3";}i:1;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.02914814815";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"4";s:2:"to";s:1:"4";}i:2;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:12:"0.0312962963";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"5";s:2:"to";s:1:"5";}i:3;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03344444444";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"6";s:2:"to";s:1:"6";}i:4;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03559259259";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"7";s:2:"to";s:1:"7";}i:5;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03774074074";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"8";s:2:"to";s:1:"8";}i:6;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03988888889";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"9";s:2:"to";s:1:"9";}i:7;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04203703704";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"10";s:2:"to";s:2:"10";}i:8;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04418518519";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"11";s:2:"to";s:2:"11";}i:9;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04633333333";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"12";s:2:"to";s:2:"12";}i:10;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04848148148";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"13";s:2:"to";s:2:"13";}i:11;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05062962963";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"14";s:2:"to";s:2:"14";}i:12;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05277777778";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"15";s:2:"to";s:2:"15";}i:13;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05492592593";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"16";s:2:"to";s:2:"16";}i:14;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05707407407";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"17";s:2:"to";s:2:"17";}i:15;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05922222222";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"18";s:2:"to";s:2:"18";}i:16;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06137037037";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"19";s:2:"to";s:2:"19";}i:17;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06351851852";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"20";s:2:"to";s:2:"20";}i:18;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06566666667";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"21";s:2:"to";s:2:"21";}i:19;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06781481481";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"22";s:2:"to";s:2:"22";}i:20;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06996296296";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"23";s:2:"to";s:2:"23";}i:21;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07211111111";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"24";s:2:"to";s:2:"24";}i:22;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07425925926";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"25";s:2:"to";s:2:"25";}i:23;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07640740741";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"26";s:2:"to";s:2:"26";}i:24;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07855555556";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"27";s:2:"to";s:2:"27";}i:25;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:12:"0.0807037037";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"28";s:2:"to";s:2:"28";}i:26;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.08285185185";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"29";s:2:"to";s:2:"29";}i:27;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:5:"0.085";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"30";s:2:"to";s:2:"30";}}');
答案 0 :(得分:2)
首先,值得注意的是,您没有进行PHP查询,而是使用WordPress函数(update_post_meta)来保存此数据。这可能就是混乱所在。
您遇到的问题是您传递的是serialized数据。在WordPress中,即使它将数据作为序列化数据存储在数据库中,您也不必自己序列化/反序列化。
WordPress存储“自动”序列化的数据。这意味着当你致电update_post_meta时,你应该简单地传入你想要保存的数组。
这是基于上述代码的 minimal 示例:
$data = array(
array(
'type' => 'blocks',
'cost' => NULL,
'modifier' => NULL,
'base_cost' => 0.027,
'base_modifier] => 'times',
'from' => 1,
'to' => 3
),
array(
'type' => 'blocks',
'cost' => NULL,
'modifier' => NULL,
'base_cost' => 0.02914814815,
'base_modifier] => 'times',
'from] => 4,
'to] => 4
)
);
update_post_meta( $product_id, '_wc_booking_pricing', $data );
然后,当您使用get_post_meta retreive 数据时,它会将作为数组返回,就像您保存它一样。
答案 1 :(得分:1)
您尝试通过update_post_meta()
的字符串是序列化数据,需要先进行反序列化。
两种可能的情况:
1)如果您已对该数据进行序列化,则不要直接在update_post_meta()
中传递数据数组。
请注意,使用
update_post_meta()
时,"传递的数组将序列化为字符串" ,因此在您的情况下,要避免出现一种双重序列化您的数据不可读。
2)如果您自己没有序列化该数据,则可能需要先对此数据进行反序列化。为此,您可以在使用update_post_meta()
之前使用WordPress函数maybe_unserialize()
,这样:
// Unserializing the data
$data = maybe_unserialize( 'a:28:{i:0;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:5:"0.027";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"1";s:2:"to";s:1:"3";}i:1;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.02914814815";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"4";s:2:"to";s:1:"4";}i:2;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:12:"0.0312962963";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"5";s:2:"to";s:1:"5";}i:3;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03344444444";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"6";s:2:"to";s:1:"6";}i:4;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03559259259";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"7";s:2:"to";s:1:"7";}i:5;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03774074074";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"8";s:2:"to";s:1:"8";}i:6;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03988888889";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"9";s:2:"to";s:1:"9";}i:7;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04203703704";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"10";s:2:"to";s:2:"10";}i:8;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04418518519";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"11";s:2:"to";s:2:"11";}i:9;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04633333333";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"12";s:2:"to";s:2:"12";}i:10;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04848148148";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"13";s:2:"to";s:2:"13";}i:11;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05062962963";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"14";s:2:"to";s:2:"14";}i:12;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05277777778";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"15";s:2:"to";s:2:"15";}i:13;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05492592593";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"16";s:2:"to";s:2:"16";}i:14;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05707407407";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"17";s:2:"to";s:2:"17";}i:15;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05922222222";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"18";s:2:"to";s:2:"18";}i:16;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06137037037";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"19";s:2:"to";s:2:"19";}i:17;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06351851852";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"20";s:2:"to";s:2:"20";}i:18;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06566666667";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"21";s:2:"to";s:2:"21";}i:19;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06781481481";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"22";s:2:"to";s:2:"22";}i:20;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06996296296";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"23";s:2:"to";s:2:"23";}i:21;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07211111111";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"24";s:2:"to";s:2:"24";}i:22;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07425925926";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"25";s:2:"to";s:2:"25";}i:23;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07640740741";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"26";s:2:"to";s:2:"26";}i:24;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07855555556";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"27";s:2:"to";s:2:"27";}i:25;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:12:"0.0807037037";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"28";s:2:"to";s:2:"28";}i:26;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.08285185185";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"29";s:2:"to";s:2:"29";}i:27;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:5:"0.085";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"30";s:2:"to";s:2:"30";}}' );
// Saving the data
update_post_meta( $product_id, '_wc_booking_pricing', $data );
现在您的数据已正确保存在数据库中,可以读取和/或处理。