0

我正在尝试使以下内容正常工作。我的 else 工作正常,但我在 if 部分遇到了问题。它所做的是删除该行并将 0 插入该hottest_cat行。它应该从 插入值$dd。关于我做错了什么的任何想法?

public function change_category() {
        $dd = $this->input->post('dd');
        $sql = $this->db->get('default_hottest_cat');
        if ($sql->num_rows() > 0) {
            $row = $sql->row(); 
                $old = $row->hottest_cat;
                // Stuff is found in this table. Needs to be deleted first, then inserted.
                $this->db->delete('default_hottest_cat', array('hottest_cat' => $old));
                $this->db->insert('default_hottest_cat', array('hottest_cat' => $dd));
        } else {
            // Nothing is found in this table. Needs only to be inserted.
            $this->db->insert('default_hottest_cat', array('hottest_cat' => $dd));
        }
    }
4

1 回答 1

0

您的问题与我回答的问题非常相似。请检查此以获取更多信息 如何检查 id 是否已存在 - codeigniter

逻辑是

1.Select "the database table" with the value you had posted.(search)
2.Check the count of result_row(if found >0 , if not found =0)
3.If not found  'insert', if found 'update',( in your case delete)

模型

<?php
    class Cars_model extends CI_Model
    {   
        function __construct()
        {
            parent:: __construct();
            $this->load->database();
        }

         function check()
         {
            $query=null; //emptying in case 

            $dd= $_POST['dd'];    //getting from post value

            $query = $this->db->get_where('default_hottest_cat', array('dd' => $dd)); ၊၊၊။။//making selection
            $count= $query->num_rows();    //counting result from query

        if ($count === 0)  // data is not present
        {  
               $data = array(
                 'dd' => $dd
            );
           $this->db->insert('default_hottest_cat', $data); 
        }
           else //data is present
          {
               $this->db->delete('default_hottest_cat', array('dd'=>$dd));
           }
         }     
    }

?>
于 2013-04-22T20:02:14.400 回答