2016-07-10 7 views
0

Ich habe ein Formular, Textfelder und einzelnes Bild haben, fügen das Textfeld in die Datenbank erfolgreich ein, aber Bildpfad fügt nicht in der Datenbank, die ich in Bildvariable gespeichert habe, ich benutze MySQL-Datenbank, das Bild wird in den Ordner hochgeladen, aber der Pfad wird nicht in der Datenbank gespeichert.Einfügen von Bild Pfad zur Datenbank in Codezeichner

Die Controller-Datei

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


     } 
     } 
} 

Die Modellklasse

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 
 
}

Antwort

0

$post['image'] = $image; nicht g o durch $this->input->post('image').

Sie können versuchen, $post['image'] als $_POST['image'].

erklärt Wenn dies nicht funktioniert, dann müssen Sie manuell in die Variable übergeben Funktion create_product.

+0

Dank einer Tonne kimberlee es funktioniert – Raahull

0

Versuchen Sie besser, Pfaddaten zur Variablen hinzuzufügen und diese an die Modellfunktion zu übergeben. Sie sehen klarer, was los ist. Also:

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

     } 

und Funktion innerhalb Modell:

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

Ich schlage vor, würde noch weiter gehen und die erste Stelle setzen alle Produktdaten in ein Array und es dann an das Modell übergeben.

0

können Sie diesen Code versuchen:

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

Modell Funktion:

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