用ajax更新而不是替换wordpress自定义字段

xsuvu9jc  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(337)

我需要在wordpress中更新一个自定义字段。我在一个图库中有几个图像,每个图像都有一个文本区域,用于评论。现在我可以在图像的文本区域中编写文本了,在db中输入文本后,文本就可以用ajax不稳定地保存了。这很管用。然而,当向另一个文本添加文本时,第一个文本再次消失。如何添加第二条评论而不删除其他评论?
db中的条目在向第一张图片添加注解时如下所示:
a:1:{i:8978;s:20:“图片评论1”;}
将注解添加到另一条注解时,整个条目如下所示:
a:1:{i:8977;s:19:“图片评论2”;}
所以它覆盖了第一个
它需要像这样:
a:2:{i:8978;s:19:“图片评论1”;一:8977;s:19:“图片评论2”;}
这就是mysql数据库

我的密码是:

if(isset($_POST['method']) && $_POST['method'] == 'comment')
{
    //Get current commentd images
    $current_images_comment = get_post_meta('9550', 'gallery_images_comment', true);

    if(!empty($current_images_comment))
    {
        if ( !in_array( $image_id, $current_images_comment ) ) {
            $current_images_comment[] = $image_id;
        }

       $current_images_comment = array_unique($current_images_comment);

       $poddata = Array( $image_id =>$comm_text );
       update_post_meta('9550', 'gallery_images_comment', $poddata);

    }

}

comm\u text包含来自textarea的文本

if(isset($_POST['comm_text']))
{
    $comm_text = $_POST['comm_text'];
}
ojsjcaue

ojsjcaue1#

好的,我用这个小代码解决了这个问题,它将新值附加到现有数组中

$poddata = $current_images_comment;
$poddata[$image_id] = $comm_text;

也许对其他人也有用

if(isset($_POST['method']) && $_POST['method'] == 'comment')
        {
            //Get current commentd images
            $current_images_comment = get_post_meta($gallery_id, 'gallery_images_comment', true);

            if(!empty($current_images_comment))
            {

      $poddata = $current_images_comment;
      $poddata[$image_id] = $comm_text;
            } else {
/*if no entry in db*/
  $poddata = Array(
      $image_id =>$comm_text
      );

}

  update_post_meta($gallery_id, 'gallery_images_comment', $poddata);
        }

相关问题