faza
faza

Reputation: 257

insert or replace in codeigniter

I want to insert of replace if exist in database with codeigniter this my model

public function upload($id_akun,$data){
               $query = $this->db->query("SELECT * FROM akun WHERE
id_akun = '{$data['id_akun']}' ");
$result = $query->result_array();
$count = count($result);

if (empty($count)) {

     $this->db->insert('foto', $data);  
}
elseif ($count == 1) {
   $this->db->where('id_akun', $data['id_akun']);
                    $this->db->update('foto', $data);  
}

I'm succesful replace(update) data if exist but it not inserting data if (empty($count))

conditional ($count == 1)---> it's ok condtional (empty($count))--->problem?

Upvotes: 1

Views: 1891

Answers (5)

Razib Al Mamun
Razib Al Mamun

Reputation: 2711

There are several benefits to using Active Record (or Query Builder as its going to be called soon):

  1. Security
  2. Code-Cleanliness
  3. Database-Agnostic

so, all time try to use active record.

<?php
public function upload($id_akun,$data){ 
    $query = $this->db->select('*')->where('id_akun', $data['id_akun'])->from('akun')->get();
    $count = $query->num_rows();
    $result = $query->result_array();

    if ($count == 0)
        return $this->db->insert('foto', $data);
    }
    elseif ($count > 1) {
        $this->db->where('id_akun', $data['id_akun']);
        return $this->db->update('foto', $data);  
    }
}

Upvotes: 0

Ahmer Saeed
Ahmer Saeed

Reputation: 596

Explanation

The function result_array() results empty array if nothing is find in fetching record. Hence when you fetch record it returns you the empty array and you are taking count of this empty array which returns you the zero length which is not equals to empty hence your condition is always remain false.

You may use the following Code.

Code

     function upload($id_akun, $data) {
       //use active record methods, otherwise, you need to sanitize input
       $this->db->select('*');
       $this->db->where('id_akun', $data['id_akun']);
       $query = $this->db->get('akun');
       if ($query->num_rows() == 0) {

           //write your Insert query here

       } elseif ($query->num_rows() > 0) {

          //write your Update query here 

       }
     }

Upvotes: 0

Abdulla Nilam
Abdulla Nilam

Reputation: 38652

Try this

In Model

public function upload($id_akun,$table){
    $query = $this->db->query("SELECT * FROM akun WHERE id_akun = '$id_akun' ");
    $result = $query->result_array();
    $count = count($result);

    if (empty($count)){
        $this->db->insert('foto', $table);
        return 0; 
    }
    elseif ($count == 1) {
        $this->db->where('id_akun', $id_akun);
        $this->db->update('foto', $table);  
        return 1;
    }
    elseif (($count >= 1) {
        return 2;
    }
}

In Controller

$id_akun = 25;
$table = 'table_name';

$result = $this->model_name->upload($id_akun,$table);

switch ($result) {
    case 0:
        $output = 'Data Inserted'
        break;
    case 1:
        $output = 'Record Inserted'
        break;
    case 2:
        $output = 'Multiple Record Found'
        break;
}

Upvotes: 2

Kisaragi
Kisaragi

Reputation: 2218

Your problem is you're checking to see if the result of a count() is empty. That will always evaluate to false.

public function upload($id_akun, $data)
{
    //use active record methods, otherwise, you need to sanitize input
    $this->db->select('*');
    $this->db->where('id_akun', $data['id_akun']);
    $query = $this->db->get('akun'); 

    if($query->num_rows() == 0){
        $this->db->update...
    }else{
         ....
    }
}

Upvotes: 0

anuraj
anuraj

Reputation: 157

You can try like this ..

public function upload($id_akun,$data){       
   $this->db->where('id_akun',$data['id_akun']);
   $this->db->from('akun');
   $query = $this->db->get();
   $rowcount = $query->num_rows();

  if ($rowcount==0) {

    $this->db->insert('foto', $data);  
  }
 else {
   $this->db->where('id_akun', $data['id_akun']);
                $this->db->update('foto', $data);  
  }

Upvotes: 0

Related Questions