0

In my page , I have created one form with different fields like name, email,password,etc., Now I have to upload the image and store in my database. Any one send the code with all fields including image upload file. I have to try in many times, image can't upload in my database.

My controller is

function activity() {
    $this->load->library('form_validation');
    //field name,error message, validation rules
    $this->form_validation->set_rules('activityid', 'Activity Id', 'trim|required');
    $this->form_validation->set_rules('hostid', 'Host Id', 'trim|required');
    $this->form_validation->set_rules('activityname', 'Activity Name', 'trim|required');
    $this->form_validation->set_rules('date', 'Date', 'trim|required');
    $this->form_validation->set_rules('venue', 'Venue', 'trim|required');
    $this->form_validation->set_rules('typeofactivity', 'Type of Activity', 'trim|required');
    $this->form_validation->set_rules('conductedby', 'Conducted By', 'trim|required');
    if ($this->form_validation->run() == FALSE) {
        //$this->load->view('signup_form');
        $this->activity_reg();
    } else {
        $this->load->model('membership_model');

        $query = $this->membership_model->activity();

        if ($query) {
            $data['main_content'] = 'signup_successful';
            $this->load->view('includes/templates', $data);
        } else {
            $this->load->view('activity');
        }
    }
}

My model is

function activity() {
    $new_member_insert_data = array(
        'activityid' => $this->input->post('activityid'),
        'hostid' => $this->input->post('hostid'),
        'activityname' => $this->input->post('activityname'),
        'date' => $this->input->post('date'),
        'venue' => $this->input->post('venue'),
        'typeofactivity' => $this->input->post('typeofactivity'),
        'conductedby' => $this->input->post('conductedby')
    );

    $this->load->database();
    $insert = $this->db->insert('activity', $new_member_insert_data);
    return $insert;
}
Roopendra
  • 7,674
  • 16
  • 65
  • 92
abu
  • 11
  • 5
  • why image to save in database? use just its name rather than images itself. – Abhishek Feb 05 '14 at 08:46
  • send me code, i will try – abu Feb 05 '14 at 08:50
  • check the below link it will give you an idea http://ellislab.com/codeigniter%20/user-guide/libraries/file_uploading.html after assigning just update the uniqueimage name in database tables – Abhishek Feb 05 '14 at 08:55
  • or just can go on this link http://stackoverflow.com/questions/288076/codeigniter-uploading-an-image-through-a-form-store-the-location-of-the-image – Abhishek Feb 05 '14 at 08:58

2 Answers2

5

You are usually better off storing the image in the filesystem and saving the PATH to the image in the database as text.

If you're deadset on using the Database, you'll need to insert the image as a BLOB.

There's a pretty good overview and tutorial here: http://www.phpro.org/tutorials/Storing-Images-in-MySQL-with-PHP.html#3

Alan Kael Ball
  • 680
  • 6
  • 17
1

you dont have to save an image in database. here is what you can do the make your app secure: when you upload your image:

  • validate image
  • secure its name in a variable
  • rename it to a standard name
  • upload it on a secure folder
  • store the image's original and renamed name in database(not the image itself)
  • when retrieving image, just define the path of the image and echo the new name.

this should work fine

OR if you really want to insert image into database, follow this page. it should give you exactly what you want!!

http://mrarrowhead.com/index.php?page=store_images_mysql_php.php

ashish
  • 3,555
  • 1
  • 20
  • 26