在codeigniter中将图像路径插入数据库

时间:2016-07-10 09:57:32

标签: php mysql codeigniter

我有一个有文本字段和单个图像的表单,文本字段成功插入数据库但图像路径没有插入我已存储在图像变量中的数据库中,我使用的是mysql数据库,图像正在上传在文件夹中但路径未存储在数据库中。

控制器文件

class product{
function validate_products()
{


        $this->form_validation->set_rules('title', 'title', 'trim|required|');
        $this->form_validation->set_rules('price', 'price', 'trim|required');
        $this->form_validation->set_rules('description', 'description', 'trim|required');
        $this->form_validation->set_rules('category', 'category', 'trim|required');
        //$this->form_validation->set_rules('image', 'image', 'trim|required');

        $config = array(
        'upload_path' => "./images/",
        'allowed_types' => "gif|jpg|png|jpeg",
        'overwrite' => false,
        );  



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



        if($this->form_validation->run() && $this->upload->do_upload() )
        {

            $data =  $this->upload->data();
            $image= base_url("images/". $data['raw_name'] . $data['file_ext']);

            $post['image'] = $image;



            $this->load->model('upload_model');

            if($query= $this->upload_model->create_product())
            {
                $this->session->set_flashdata('product_sucsess', 'Product uploaded sucsessfully');
                $this->upload();

            }
            else
            {


                $this->session->set_flashdata('product_fail', 'Sorry product not uploaded .');
                $this->load->view('Admin/upload_product');

            }


        }
        else
        {

            $error = array('error' => $this->upload->display_errors());
            $this->load->view('admin/upload_product', $error);


        }
        }
}

模型类

class upload_model extends  CI_Model
{
	public function __construct() 
     {
           parent::__construct(); 
           $this->load->database();
     }

	
	
	function create_product()
	{
		$new_member_insert_data = array(
		'title'=> $this->input->post('title'),
		'image'=> $this->input->post('image'),
		'price'=> $this->input->post('price'),
		'description'=> $this->input->post('description'),
		'category'=> $this->input->post('category'));
		
		$insert = $this->db->insert('product', $new_member_insert_data);
		return true;
	}
	
}

1 个答案:

答案 0 :(得分:0)

@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> <script src="~/Scripts/jquery-1.10.2.min.js"></script> <script type="text/javascript"> function f1() { var token=""; $.ajax({ data: { webservice_username: 'webservice_usernam', password: 'password' }, url: 'http://www.travelinsure.ir/api/v1.1/authenticate', type: 'POST', dataType: 'json', success: function (data) { $.each(data, function (idx, result) { token = result; f2(result); }) }, error: function (x, y, z) { alert(x.responseText); alert(z); } }); } function f2(token) { // alert('New: '+token); $.ajax({ data: { token: token }, url: 'http://www.travelinsure.ir/api/v1.1/getcitylist?token=', type: 'GET', dataType: 'json', success: function (data) { results = JSON.stringify(data); // a = JSON.parse(data); alert(results); a = $.parseJSON(data); alert(a); $.each(data, function (idx, r) { $("#sel").append("<option value='" + r.city_id + "'>'" + r.city_name + "'</option>"); alert(result.city_name); }) }, error: function (x, y, z) { alert(x.responseText); alert(z); } }); } </script> </head> <body> <div> <input type="submit" name="name" value="Click!!!" onclick="f1()" /> </div> <div id="mydiv"> <select id="sel"> <option>لیست شهرهای ایران</option> </select> </div> </body> </html> 未通过$post['image'] = $image;

您可以尝试将$this->input->post('image').声明为$post['image']

如果这不起作用,则需要手动将变量传递给create_product函数。