【问题标题】:Save Image Name And Post Data Into Database将图像名称和发布数据保存到数据库中
【发布时间】:2015-07-17 22:05:36
【问题描述】:

我正在做一个志愿者必须填写注册表的项目。

我希望志愿者能够上传个人资料图片并将图片名称保存到数据库以及同一表格上的帖子数据。

我怎样才能做到这一点?

【问题讨论】:

  • 你的代码在哪里以及你为它尝试了什么?
  • 您要上传并存储在数据库中吗??
  • 将 jpeg 文件的名称存储在数据库中并将图像保存到某个位置,这就是它的完成方式

标签: codeigniter


【解决方案1】:

在上传表单的成功部分可以执行如下 if 语句进行更新

更新

$user_info = $this->get_user();

if ($user_info) {

$image_info = $this->upload->data();

$data = array(
'username' => $user_info['username'],
'image' => $image_info['file_name']
);

// Only updates password if post value true
if(isset($_POST['password'])) {
   $data = array(
      'password' => $this->input->post('password');
   );
}


$this->db->where('id', $user_info['id']);
$this->db->update('tablename', $data);

}

否则在上传表单的成功部分可以像下面这样插入

插入

$image_info = $this->upload->data();

$data = array(
   'username' => $this->input->post('username'),
   'password' => $this->input->post('password')
   'image' => $image_info['file_name']
);


$this->db->insert('tablename', $data);

上传控制器

<?php

class Upload extends CI_Controller {

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

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

    function do_upload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '0'; // Unlimited
        $config['max_width']  = '0'; // Unlimited
        $config['max_height']  = '0'; // Unlimited

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

        // Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class:
        $this->upload->initialize($config);

        $input_name = "userfile";

        if ( ! $this->upload->do_upload($input_name))
        {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_form', $error);
        }
        else
        {


            $user_info = $this->get_user();

            if ($user_info) {

                $image_info = $this->upload_data();

                $data = array(
                    'username' => $user_info['username'],
                    'image' => $image_info['file_name']
                );

                $this->db->where('id', $user_info['id']);
                $this->db->update('tablename', $data);

            }

            $this->load->view('upload_success', $data);
        }
    }

    public function get_user() {

        // your user model data

    }
}
?>

查看

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

<?php echo $error;?>

<?php echo form_open_multipart('upload/do_upload');?>
<input type="text" name="name"/>
<input type="password" name="password"/>
<input type="file" name="userfile" size="20" />

<br /><br />

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

</form>

</body>
</html>

上传库

Codeigniter 2 http://www.codeigniter.com/userguide2/libraries/file_uploading.html

Codeigniter 3 http://www.codeigniter.com/user_guide/libraries/file_uploading.html

【讨论】:

  • 我认为(至少在 codeigniter 3 中)this-&gt;upload_data() 应该是 this-&gt;upload-&gt;data();
  • @mheavers 只是一个错字
【解决方案2】:

你的问题不清楚,

同时提供、视图、控制器。 不管怎样,看看这篇文章会对你有所帮助。

click here

使用&lt;?php echo form_open_multipart('yourcontroler/add');?&gt; 在处理图像和文本数据时在您的视图文件中。

在控制器中,最好的做法是定义两个函数,一个用于图像上传,另一个用于验证和过滤数据。喜欢

             public function do_upload() {
                $path = '_assets/images/';
                $fileName = 'userpic.png';
                $config = array(
                    'allowed_types' => 'jpg|png|gif',
                    'upload_path' => $path,
                    'max_size' => '200',
                    'overwrite' => true,
                    'file_name' => $fileName,
                    'max_height' => "300",
                    'max_width' => "300",

                );

                $this->load->library('upload', $config);
                if (!$this->upload->do_upload('logo')) {
                    echo "error ";
                    die;
                } else {
                    $imageData = $this->upload->data();
                }

            }

对于数据存储,请使用通常对表单输入执行的简单功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-26
    • 2017-04-22
    • 2015-07-10
    • 1970-01-01
    • 2020-03-07
    相关资源
    最近更新 更多