php - 在Codeigniter中的同一个mysql表中上传图像和文本

标签 php mysql codeigniter

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

Controller 上的代码

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,第二个是数组,您没有在插入函数中发送数据,看看它应该是这样的。

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


      }
 }

关于php - 在Codeigniter中的同一个mysql表中上传图像和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29238262/

相关文章:

php - 如何编写查询来查找不属于所选字段的记录?

php - 选择范围内最大值的行

mysql - 获取mysql表中的最后一条记录

sql - 使用 codeigniter 和 ajax 检查数据库中是否存在用户名

PHP & MySQL : unexpected T_VARIABLE

php - 发送多个相同的表格?

mysql - mysql 是否可以动态翻译存储在字符串中的 ID?

php - 无法使用 PHP 从数据库获取正确的字段

php - 为什么我在 View 中的显示 Controller 数组中出现错误 undefined variable ?

php - 在后台进程中运行 bash 文件问题