avenirer / MY_Upload

An addition to the CI_Upload, that allows for multiple files upload.
38 stars 23 forks source link

Pass data to database #3

Closed iamthestreets closed 8 years ago

iamthestreets commented 8 years ago

Hi,

First, thank you for this library!

I am trying to pass the image data to the database, but I can't seem to accomplish this without either getting an error or only inserting the last image.

Here is my code:

if ( ! $this->upload->do_upload('post_image'))
                {
                    $this->session->set_flashdata('post_message', $this->upload->display_errors());
                    $this->session->set_flashdata('post_message_class', 'alert-danger');
                    redirect('/user/profile/'.$identity, 'refresh');
                }
                else
                {
                    $uploaded = $this->upload->data();                          
                    // insert pos
                    if(isset($uploaded['file_name']) && $uploaded['file_name'] == 0){                       
                        $post_text = '<img src="/uploads/images/'.$uploaded['file_name'].'" width="251px" alt="'.$uploaded['raw_name'].'" /><br>'.$this->input->post('post_text');                                  
                    }
                    else 
                    {
                        foreach ($uploaded as $images){                                             
                            $post_text = '<img src="/uploads/images/'.$images['file_name'].'" width="251px" alt="'.$images['raw_name'].'" /><br>'.$this->input->post('post_text');                                                  
                        }   
                    }               

                    $query = $this->user_model->insert_user_posts($this->input->post('poster_id'), $this->input->post('profile_id'), $this->input->post('post_type'), $post_text);
                    $this->session->set_flashdata('post_message', 'Image has been posted!');
                    $this->session->set_flashdata('post_message_class', 'alert-success');
                    redirect('/user/profile/'.$identity, 'refresh');          
                }

Here is the code to the model:

public function insert_user_posts($poster_id, $profile_id, $post_type, $post_text)
    {
        // Users table.
        $data = array(
            'poster_id'     => $poster_id,
            'profile_id'    => $profile_id,
            'post_type'     => $post_type,
            'post_text'     => $post_text,
            'datetime'      => time()
        );

        $this->db->insert($this->tables['users_posts'], $data);

        if($this->db->affected_rows() > 0)
        {
            $this->set_message('login_successful');
            return TRUE;
        }
        else
        {
            return FALSE;   
        }
    }

If I echo or var_dump the $post_text it will show the data for both images, but it is only inserting the 1 image.

I want to append them together in the same $post_text variable to be inserted together.

What am I doing wrong here?

avenirer commented 8 years ago

As you can see, the upload library works. About inserting data in a database... well... that is another story not related to the library. So please, do not use "Report issue" for something that is not an issue related to the project.