【问题标题】:where do I put insert db query codeigniter我在哪里放置插入数据库查询代码点火器
【发布时间】:2014-09-16 07:03:46
【问题描述】:

在我的控制器中,我有以下功能可以将图像上传到本地目录,效果很好。我现在想存储在数据库中。我只需要弄清楚我已经拥有并认为正确的代码应该放在哪里。

我需要插入这个:

  $this->location->store_file_path($file);

在图片上传到本地目录之前或之后。应该去哪里?

控制器:

public function image() {

    //type must either be type=logo or type=profile_picture

    if (isset($_FILES['file']) && isset($_POST['type'])) {

        //Codeigniter upload
        $config['upload_path'] = 'uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['encrypt_name'] = TRUE;
        $config['max_size'] = 5120;
        $config['max_width'] = '1024';
        $config['max_height'] = '768';
        $this->upload->initialize($config);

        if (!$this->upload->do_upload('file')) {

            $status = 'error';
            $msg = $this->upload->display_errors('', '');
        } else {
            $data = $this->upload->data();
             //$file_id = $this->files_model->insert_file($data['file_name'], $_POST['title']);
            if ($data) {
                $status = "success";
                $data['url'] = $config['upload_path'] . $data['file_name'];
                $msg = $data;
            } else {
                unlink($data['full_path']);
                $status = "error";
                $msg = "Something went wrong when saving the file, please try again.";
            }


        }
    } else {
        $status = "error";
        $msg = "Please select a file to upload";
    }
    echo json_encode(array('status' => $status, 'msg' => $msg));
}

这是我的模型函数:

public function store_file_path($file)
{

    $this->db->insert('TACTIFY_locationcards', 'name'=>$file);
}

【问题讨论】:

  • 刚刚编辑和更新的参数通过了。
  • 就像使用活动记录一样正常插入它,你的情况没有什么特别的
  • 只是个人提示,虽然它是您的应用程序,并且您可以将代码放置在您想要的位置(您不需要使用模型),最好将上传代码放置在模型中同样,也许作为另一种方法。这是因为如果您决定为同一目的使用另一种上传方法,则需要在控制器中复制上传代码。放置它并从模型中调用相同的代码将减少重复。
  • 谢谢。那是真实的。但是,这不是我的应用程序,我必须处处处理意大利面条代码。
  • 我插入并测试了,但出于某种奇怪的原因,它破坏了我的裁剪功能,这没有任何意义。

标签: php codeigniter


【解决方案1】:

在控制器中更改以下行

$status = "success";
 + $file = $config['upload_path'] . $data['file_name'];
 + $this->location->store_file_path($file);
 $msg = $file;

更换型号

public function store_file_path($file)
{
   + $this->db->set('name', $file); 
   + $this->db->insert('TACTIFY_locationcards'); 
   - $this->db->insert('TACTIFY_locationcards', 'name'=>$file);
}

【讨论】:

    【解决方案2】:

    我建议您仅在完成所有验证并将数据写入文件后才写入数据库。

    如果我正确阅读了您的代码,那么成功就是:

               if ($data) {
                $status = "success";
                $data['url'] = $config['upload_path'] . $data['file_name'];
                $msg = $data;
    

    我会把这条线放在这里:

               if ($data) {
                $status = "success";
                $data['url'] = $config['upload_path'] . $data['file_name'];
                $msg = $data;
                $this->location->store_file_path($file);
    

    【讨论】:

      猜你喜欢
      • 2012-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多