douxuan0698
2014-01-02 14:25
浏览 11
已采纳

如果Codeigniter不存在则插入

I am encountering the following problem I am trying to insert data into the table if phone, sushi_service_id both has a row with active = 1 for example:

ID | Phone | sushi_service_id | active
---------------------------------------
1  | 455   | 1                | 1 (LEGAL)
3  | 455   | 1                | 0 (LEGAL)
4  | 455   | 1                | 0 (LEGAL)
5  | 455   | 1                | 1  (NOT LEGAL! HAS THE SAME PHONE+SUSHI_SERVICE_ID+ACTIVE=1)

This is my codeigniter code: how can I improve it in order to work like I said above?

public function add($service, $phone, $sushi_subscription_id, $answer_id, $affiliate_id, $ip, $query, $invite_type, $invite_msg_id)
{

    $this->db->set('service_id', $service->id);
    $this->db->set('sushi_service_id', $service->sushi_service_id);
    $this->db->set('phone', $phone);

    if ($sushi_subscription_id) {
        $this->db->set('sushi_subscription_id', $sushi_subscription_id);
    }

    $this->db->set('answer_id', $answer_id);
    if ($affiliate_id) {
        $this->db->set('affiliate_id', $affiliate_id);
    }

    $this->db->set('added', 'NOW()', FALSE);

    $this->db->set('active', 1);
    $this->db->set('ip', $ip);

    $this->db->set('query', $query);

    if ($invite_type) {
        $this->db->set('invite_type', $invite_type);
    }

    if ($invite_msg_id) {
        $this->db->set('invite_msg_id', $invite_msg_id);
    }

    return ($this->db->insert($this->_table_name)) ? $this->db->insert_id() : FALSE;
}
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题