0

I am trying to insert an image and data to the database from a form validation method using php codeigniter . Here is my code,problem is will not insert the data and image to the database. Please help. Controller Page

public function gallery5()
{  
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->library('upload');
$this->load->model('Login_set');
$page_id =$this->uri->segment(3);   
$data['e']=$this->Login_set->select1(); 
$this->load->view('App_stay/pages/hotel1_galery_event.php',$data);
}  
public function gallery5_insert()
{
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('Login_set');
$this->form_validation->set_rules('id','id');
$this->form_validation->set_rules('events','events','required');
$this->form_validation->set_rules('description','description','required');
if ($this->form_validation->run() == true)
{       
$config['upload_path']          = 'application/views/App_stay/photo1/';
$config['max_size']             = '100';
$config['max_width']            = '1024';
$config['max_height']           = '768';
$this->load->library('upload', $config);
$field_name = "images";
if ( ! $this->upload->do_upload($field_name))
{
$error = array('error' => $this->upload->display_errors());
$page_id =$this->uri->segment(3);
$data['e']=$this->Login_set->select1_edit();       
$this->load->view('App_stay/pages/hotel1_galery_event.php',$data, $error);
}
else 
{
$image_path = $this->upload->data();
$this->Login_set->add_events();
$page_id =$this->uri->segment(3);
$data['e']=$this->Login_set->select1_edit();      
$this->load->view('App_stay/pages/hotel1_galery_event.php',$data);
}
} 
} 

View Page

<form action="<?php echo base_url();?>index.php/Welcome/gallery5_insert" method="post" enctype="multipart/form-data">
<?php
foreach ($e->result_array() as $value){       
?>     
<div class="form-group has-info ">
<input type="hidden" name="id" value="<?php echo $value['event_id'];?>">
<?php }?>              
<div class="form-group has-info col-lg-7">
<label class="control-label" for="inputSuccess">Event title</label>
<input type="text" class="form-control" name="events" id="events" >
</div>                                   
<div class="form-group has-info col-lg-7 ">
<label>Event Description</label>
<textarea id="description" name="description" class="form-control " rows="3"></textarea><br><br>
</div>
<div class="form-group has-info col-lg-7">
<label>Event Related images</label>
<input type="file" name="images"><br>
</div>
<div class="form-group has-info col-lg-7">
<button type="submit" class="btn btn-primary">
<span>SUBMIT</span>
</button>
</div>
</div>     
</form> 

Model page

public function add_events()
{ 
$this->form_validation->set_message('events','events','required');
$this->form_validation->set_message('description','description','required');
$this->form_validation->set_message('images','images','required');
$this->load->database();
$this->load->helper('url');
$data = array(
'event_id'=>'',
'hotel_id'=>1,
'event_title'=>$this->input->post('events'),
'event_description'=>$this->input->post('description'),
'image_path'   =>    $image_path[full_path]                       
);
$this->db->insert('event_hotel1', $data);
}
3
  • Do you get an error? We need more information if you want help... Commented Dec 29, 2015 at 9:52
  • No same page itself occur Commented Dec 29, 2015 at 9:56
  • if you want to set rules for form validation use set_rules not set_message Commented Dec 29, 2015 at 12:42

2 Answers 2

1

You need to pass $image_path to you model file

So change

$this->Login_set->add_events();

To

$this->Login_set->add_events($image_path);// pass your argument

And in MODEL FILE

public function add_events($image_path){......
Sign up to request clarification or add additional context in comments.

1 Comment

And assign quote $image_path['full_path']
0

A few things you can fix here to make your life a bit easier to manage. Firstly, adding a __construct() function to load common libraries, helpers, and models will save you a lot of duplicate code. If you aren't familiar, a __construct() function within a Class object will run each time that class is initialized, so it's common to load your assets there, etc.

Note that the .php has also been removed from the $this->load->view() functions as it is not necessary.

We're also passing your image path using the [full_path] attribute from do_upload() to your add_events() function in your model.

Your Controller

/**
* Use a __construct function so you don't have to repeat things
*/
public function __construct() {
    // Helpers & Libraries
    $this->load->helper('url');
    $this->load->helper('form');
    $this->load->library('form_validation');

    // Models
    $this->load->model('Login_set');

    // Upload Library & Config
    $config['upload_path'] = 'application/views/App_stay/photo1/';
    $config['max_size']    = '100';
    $config['max_width']   = '1024';
    $config['max_height']  = '768';
    $this->load->library('upload', $config);
}

// Get the Gallery
public function gallery5()
{  
    $page_id =$this->uri->segment(3);   
    $data['e']=$this->Login_set->select1(); 
    $this->load->view('App_stay/pages/hotel1_galery_event',$data);
}

public function gallery5_insert()
{
    $this->form_validation->set_rules('id','id');
    $this->form_validation->set_rules('events','events','required');
    $this->form_validation->set_rules('description','description','required');

    if ($this->form_validation->run() == true)
    {   
        $field_name = "images";

        if ( ! $this->upload->do_upload($field_name))
        {
            $error = array('error' => $this->upload->display_errors());
            $page_id =$this->uri->segment(3);
            $data['e']=$this->Login_set->select1_edit();       
            $this->load->view('App_stay/pages/hotel1_galery_event',$data, $error);
        }
        else 
        {
            $image_upload = $this->upload->data();
            $this->Login_set->add_events($image_upload['full_path']);
            $page_id =$this->uri->segment(3);
            $data['e']=$this->Login_set->select1_edit();      
            $this->load->view('App_stay/pages/hotel1_galery_event',$data);
        }
    } 
} 

Your Model

/**
* Add a __construct()
*/
public function __construct() {
    $this->load->database();
    $this->load->helper('url');
}

public function add_events( $image_path = '' )
{ 
    // NOTE: you don't seem to be doing anything with this
    // Also, these should be set_rules, not set_message
    $this->form_validation->set_rules('events','events','required');
    $this->form_validation->set_rules('description','description','required');
    $this->form_validation->set_rules('images','images','required');

    $data = array(
        'event_id'=>'',
        'hotel_id'=>1,
        'event_title'=>$this->input->post('events'),
        'event_description'=>$this->input->post('description'),
        'image_path'   =>    $image_path                      
    );
    $this->db->insert('event_hotel1', $data);
}

For some additional information on using set_rules, set_message, and the CI form_validation library, check out: https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.