2016-07-10 2 views
0

J'ai un formulaire qui ont des champs de texte et une image unique, le champ de texte insèrent à la base de données avec succès mais le chemin de l'image n'insère pas dans la base de données base de données mysql, l'image est en train de télécharger dans le dossier, mais le chemin n'est pas stocké dans la base de données.insérer le chemin d'image à base de données dans codeigniter

Le fichier contrôleur

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); 


     } 
     } 
} 

La classe modèle

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

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

Répondre

0

$post['image'] = $image; ne le fait pas g o par $this->input->post('image').

Vous pouvez déclarer $post['image'] comme $_POST['image'].

Si cela ne fonctionne pas, alors vous aurez besoin de passer manuellement dans la variable à create_product fonction.

+0

Merci une tonne kimberlee il travaille – Raahull

0

Essayez mieux d'ajouter des données de chemin à la variable et passez-la à la fonction de modèle. Vous voyez plus clairement ce qui se passe. Alors:

$upload_data = $this->upload->data(); 
$uploaded_image_path = $upload_data['full_path']; 


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

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

     } 

et la fonction dans le modèle:

function create_product($upload_path) 
    { 
     $new_member_insert_data = array(
     'title'=> $this->input->post('title'), 
     'image'=> $upload_path, 
     '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; 
    } 

Je suggère d'aller plus loin et d'abord mettre toutes les données produit dans un tableau et ensuite passer au modèle.

0

Vous pouvez essayer ce code:

if ($this->form_validation->run()) 
{ 
    if (! $this->upload->do_upload('upload_file')) 
    { 
     $data['error'] = $this->upload->display_errors(); 
     //print_r($data);die('error'); 
    } 
    else 
    { 
     $data['upload_data'] = $this->upload->data(); 
     //print_r($data);die(); 
     $file_name = $this->upload->data('file_name'); 
     $image_url = $this->upload->data('full_path'); 

    $image_info = array('title'=> $this->input->post('title'), 
         'price'=> $this->input->post('price'), 
         'description'=> $this->input->post('description'), 
         'category'=> $this->input->post('category'), 
         'image_name' => $file_name, 
         'image_url' => $image_url); 
    //pr($image_info);die; 
    $insert_id = $this->upload_model->create_product($image_info); 
    if($insert_id) 
    { 
     $this->session->set_flashdata('image added',array('message' => 'image is uploaded successfully!')); 
     redirect('Controller/method_name', 'refresh'); 
    } 
    } 
} 

fonction du modèle:

function create_product($image_info) 
    { 
    $insert = $this->db->insert('product', $image_info); 
    return $insert; 
}