[英]codeigniter Check if data exist in database update table row
How to check if a column exists in query builder Codeigniter 3? 如何检查查询构建器Codeigniter 3中是否存在列? my database table name is "tags" and this table have two column (post_id and tag) here is my code to add tags to post:
我的数据库表名是“tags”,这个表有两列(post_id和tag)这里是我添加标签的代码:
//add post tags
public function add_post_tags($post_id)
{
//tags
$tags = trim($this->input->post('tags', true));
$tags_array = explode(",", $tags);
if (!empty($tags_array)) {
foreach ($tags_array as $tag) {
$tag = trim($tag);
if (strlen($tag) > 1) {
$data = array(
'post_id' => $post_id,
'tag' => trim($tag),
'tag_slug' => str_slug(trim($tag))
);
if (empty($data["tag_slug"]) || $data["tag_slug"] == "-") {
$data["tag_slug"] = "tag-" . uniqid();
}
//insert tag
$this->db->insert('tags', $data);
}
}
}
}
I want to check if data exists in the database update the post_id column if not exist insert new data "post_id" column like "5,8,9" I want to add the post number to the previous numbers when I update "post_id" column for example after update "post_id" column This table is like this "5,8,9,11" here 11 is my last post id 我想检查数据库中是否存在数据更新post_id列如果不存在则插入新数据“post_id”列如“5,8,9”我想在更新“post_id”列时将帖子编号添加到之前的数字例如更新后的“post_id”列这个表就像这个“5,8,9,11”这里11是我的上一篇帖子ID
Try using replace()
instead : 尝试使用
replace()
代替:
$this->db->replace('tags', $data);
Basically it inserts a new record if it doesn't exist, it updates it according to its primary or unique key if it does exist. 基本上,如果它不存在,它会插入一条新记录,如果它存在,它会根据其主键或唯一键更新它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.