Php 插入或更换编码点火器

Php 插入或更换编码点火器,php,codeigniter,Php,Codeigniter,如果数据库中存在codeigniter,我想插入替换 这是我的模型 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 (e

如果数据库中存在codeigniter,我想插入替换 这是我的模型

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);  
}
如果存在数据,我会成功地替换(更新)数据,但如果(空($count))则不会插入数据

有条件($count==1)--->没问题
条件(空($count))-->问题?

您可以这样尝试

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

您的问题是正在检查
count()
的结果是否为空。这将永远被评估为错误

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{
         ....
    }
}

解释

函数result\u array()如果在获取记录中未找到任何内容,则会生成空数组。因此,当您获取记录时,它将返回空数组,并且您正在计算该空数组,该空数组返回的长度为零,不等于空,因此您的条件始终为false

您可以使用以下代码

代码

     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 

       }
     }
试试这个

在型号中

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;
    }
}
在控制器中

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

使用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);  
        }
    }