douxie1692 2015-06-09 12:56
浏览 46

将数据库内容从一个表复制到codeigniter中的另一个表

I need to copy table contents from table: user_openletter to table: country on clicking archive button. How to write a query to copy from,to,title,openletter,open_id from user_openletter to another table country. This a codeigniter app.

my controller code is :

 public function store_user_data_archieve()       
 {
    $match = $this->input->post('submit');
    echo $match;
    if($match == "Archieve")
    {
        $data = array(
         'open_id' => $this->input->post('open_id'),
         'featured' => '1',
         'from' => $this->input->post('from'),
             'to' => $this->input->post('to'),
             'title' => $this->input->post('title'),

             'archieve' => '1',
             'latest' => '0',
             'sponsor' => 'images/sponsor.png'

           );
      $this->load->database();  
         //load the model  
         $this->load->model('select');  
         //load the method of model  
         $data['r']=$this->select->store_user_data_archieve($data);  
        echo "success";
        }
        else if(!$match == "new")
        {
            $data = array(
         'open_id' => $this->input->post('open_id'),
         'featured' => '1',
         'from' => $this->input->post('from'),
             'to' => $this->input->post('to'),
             'title' => $this->input->post('title'),
             'openletter' => $this->input->post('openletter'),
             'archieve' => '0',
             'latest' => '1',
             'sponsor' => 'images/sponsor.png'

           );
      $this->load->database();  
         //load the model  
         $this->load->model('select');  
         //load the method of model  
         $data['r']=$this->select->store_user_data_archieve($data);  
        echo "success";

        }
           else if(!$match == "Discard")
        {

        echo "failure";

        }

      }

My view code is:

<?php  
         foreach ($r->result() as $row)  
         {  
        ?>

              <table border="1" cellpadding="4" cellspacing="0">
                <tr>
                    <td>from</td>
                    <td>to</td>
                    <td>title</td>
                    <td>openletter</td>
                    <td>Date & Time</td>
                    <td>open_id</td>
                </tr>

                    <tr>
                 <form action="/index.php/welcome/store_user_data_archieve" method="post">
                        <td><input type="text" name="from" value="<?php echo $row->from;?>" /></td>
                    <td><input type="text" name="to" value="<?php echo $row->to;?>" /></td>
                    <td><input type="text" name="title" value="<?php echo $row->title;?>" /></td>
                    <td><input type="text" name="openletter" value="<?php echo $row->openletter;?>" /></td>
                    <td><input type="text" name="datetime" value="<?php echo $row->datetime;?>" /></td>
                    <td><input type="text" name="open_id" value="<?php echo $row->open_id;?>" /></td>
                    <td><div><input type="submit" name="submit" value="Archieve" /></div></td>
                    <td><div><input type="submit" name="new" value="new" /></div></td>

                </form>
                </tr>
              </table>

         <?php } ?>

My model code is:

public function store_user_data_archieve($data)  
{  
 //data is retrieved from this query
$this->db->insert('country', $data);
$this->db->set('openletter');       
$this->db->select('openletter');
$this->db->where('open_id', $data[open_id]); 
$this->db->from('user_openletter');

// Produces: INSERT INTO mytable (title, name, date) VALUES ('{$title}', '{$name}', '{$date}')
}  
  • 写回答

1条回答 默认 最新

  • douningzhi1991 2015-06-09 13:09
    关注

    As per our discussion you need to copy from,to,title,openletter,open_id from table user_openletter to country table

    $this->db->select('from,to,title,openletter,open_id');// select your filed
    $q = $this->db->get('user_openletter')->result(); // get result from table
    foreach ($q as $r) { // loop over results
            $this->db->insert('country', $r); // insert each row to country table
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP