【问题标题】:Updating and not replacing wordpress custom fields with ajax用 ajax 更新而不是替换 wordpress 自定义字段
【发布时间】:2019-05-07 08:05:46
【问题描述】:

我需要更新 wordpress 中的自定义字段。我在画廊中有几张图片,每张图片都附有 cmets 的 textarea。现在我可以在图像的文本区域中写入文本,并且在输入数据库后,文本会立即使用 ajax 保存。这样可行。但是,当将文本添加到另一个文本时,第一个文本会再次消失。如何在不删除其他评论的情况下添加第二条评论?

给第一张图片添加评论时,数据库中的条目看起来像:

a:1:{i:8978;s:20:"图片注释 1";}

向另一个添加评论时,整个条目如下所示:

a:1:{i:8977;s:19:"Picture Comment no2";}

所以它会覆盖第一个

它需要看起来像这样:

a:2:{i:8978;s:19:"图片注释 no1";i:8977;s:19:"图片注释 no2";}

这就是 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_text 包含来自 textarea 的文本

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

【问题讨论】:

  • 我添加了设置 $comm_text 的位置。然而,这个变量只包含文本,例如“Picture Comment no2”
  • 啊好的,是的,问题在于它替换了 gallery_images_comment 的值。我该如何解决这个问题并添加新值而不是替换它?我在另一个主题的函数中看到了这一点,他们通过首先读取所有 gallery_images_comment 然后附加新值来更新
  • 你能帮助我并告诉我该怎么做吗? $current_images_comment 包含以前的 cmets
  • 啊哈,所以我现在应该在这个平台上付钱给你,你给我一个我可以使用的功能的提示?
  • 好的,我们等着有人帮忙

标签: php mysql arrays ajax wordpress


【解决方案1】:

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

$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);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    相关资源
    最近更新 更多