我正在尝试在codeigniter的同一个mysql表中上传图像和文本,但出现数据库错误,例如“您必须使用“ set”方法来更新条目。“

控制器上的代码
    

class Addnews extends CI_Controller {

function __construct()
{
    parent::__construct();
    $this->load->helper(array('form', 'url'));
}

function index()
{
    $this->load->view('addnews', array('error' => ' ' ));
}

function do_upload()
{
    $config['upload_path'] = './assets/images/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1040';
    $config['max_height']  = '1040';

    $this->load->library('upload', $config);
    $this->upload->initialize($config);

    $newRow = array("news_title" => $this->input->post('news_title'),
                    "news_description" => $this->input->post('news_description'));
            $data = array('upload' => $this->upload->data());

        $result = array_merge($newRow, $data);

    if ( ! $this->upload->do_upload())
    {
         $image_data = $this->upload->data();
       $newRow['imgpath'] ='assets/images/'.$image_data['file_name'];
        $this->load->view('addnews');
    }
    else
    {

    $this->load->model("modeladdnews");
    $this->modeladdnews->insert_news($result);

 $this->load->view('success');
    }
}
}
?>


模型上的代码

<?php
class Modeladdnews extends CI_Model {
  function insert_news($result)
  {
     $this->db->insert('news');

  }
  }
  ?>


查看代码

<html>
<head>
<title>Upload Form</title>
</head>
<body>


 <?php echo form_open_multipart('Addnews/do_upload');?>
 <?php
  echo form_input("news_title", "");
  echo form_input("news_description", "");
  echo form_upload("userfile");
  ?>
  <br /><br />

 <input type="submit" value="submit" />

 </form>

  </body>
  </html>

最佳答案

CI活动记录类的插入方法接受两个参数,第一个是table_name,第二个是数组,您没有在insert函数中发送数据,应该是这样的。

class Modeladdnews extends CI_Model {
      function insert_news($result)
      {
         $this->db->insert('news',$result);


      }
 }

09-13 04:49