简体   繁体   English

在自定义WordPress数据库表中的文本字段中添加新行,而不会覆盖现有值

[英]Adding a new line to a text field in custom WordPress database table without overwriting the existing value

I'm trying to add data to a text field, in a custom WordPress database table, on a new line without overwriting the value that is already in there. 我正在尝试在新行中将数据添加到自定义WordPress数据库表中的文本字段中,而不覆盖其中已经存在的值。

I'm thinking it should go somewhat like this: 我认为它应该像这样:

Get the value that is currently in the field, Add a line break to it, Concatenate to the new value, Update the table field with the new value. 获取该字段中当前的值,为其添加换行符,并连接到新值,并使用新值更新表字段。

The thing is, I have the new value being posted from a form. 问题是,我从表单中发布了新值。

My current code looks like this: 我当前的代码如下所示:

$tablename = 'st_support_tickets';

$data = array(
    'ticket_comment' => $_POST['ticket_comment']);

$where = array('ID' => $ticket_id);

$wpdb->update($tablename, $data, $where);
}

I'm not sure how I should go about getting the value that is already in the 'ticket_comment' field and add it to the value that is being POST by the form. 我不确定如何获取“ ticket_comment”字段中已经存在的值,并将其添加到通过表单进行POST的值中。

Any ideas? 有任何想法吗?

Something like the below should work (not tested code, check for syntax issues) 像下面这样的东西应该可以工作(未经测试的代码,请检查语法问题)

$old_ticket_comment = $wpdb->get_var( "SELECT ticket_comment FROM $wpdb->st_support_tickets" );

$submitted_ticket_comment = $POST_['ticket_comment']; //you may want to sanitize

$new_ticket_comment = $old_ticket_comment.'<br />'.$submitted_ticket_comment;

$data = array('ticket_comment'=>$new_ticket_comment);
$where = array('ID' => $ticket_id);

$wpdb->update($tablename, $data, $where);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM