I have a custom php function for my multiple upload function. I cannot figure out how to make it so when I insert the file name to the database that it matches the row id, of that insert.

How can I make it so. When I create a new banner that each image multiple images that each match the there inserted id.

**
Model Function**

public function addBanner($data) {
        $this->db->query("INSERT INTO " . $this->db->dbprefix . "banner 
            SET name = " . $this->db->escape($data['name']) . ", 
            status = '" . (int)$data['status'] . "'
        ");

        $banner_id = $this->get_id->get_last_id();

        if (isset($data['banner_image'])) {
            foreach ($data['banner_image'] as $banner_image) {

                // Image File Not Working Need Make It
                // So When New Image It Matches The Banner_Image_ID

                $this->db->query("INSERT INTO " . $this->db->dbprefix . "banner_image 
                    SET banner_id = '" . (int)$banner_id . "', 
                    link = " .  $this->db->escape($banner_image['link']) . ",
                    image = " .  $_FILES['userfile']['name'] . " 
                ");

                $banner_image_id = $this->get_id->get_last_id();

                $this->db->query("INSERT INTO " . $this->db->dbprefix . "banner_image_description 
                    SET banner_image_id = '" . (int)$banner_image_id . "',
                    banner_id = '" . (int)$banner_id . "',
                    title = " .  $this->db->escape($banner_image['title']) . "
                ");
            }
        }
    }

CI Controller Function

public function do_upload_banner() {
        $this->lang->load('admin/design/banners', 'english');

        $valid_formats = array(
            "jpg", 
            "png", 
            "gif", 
            "zip", 
            "bmp",
            'ico'
        );

        $max_file_size = 1024*$this->settings->get('config_file_max_size');

        $path = FCPATH . "upload/"; // Upload directory

        $count = 0;

        if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){

            // Loop $_FILES to exeicute all files

            foreach ($_FILES['userfile']['name'] as $field => $name) {  

                if ($_FILES['userfile']['error'][$field] == 4) {

                    continue; // Skip file if any error found

                }          

                if ($_FILES['userfile']['error'][$field] == 0) {         

                    if ($_FILES['userfile']['size'][$field] > $max_file_size) {

                        $this->form_validation->set_message('do_upload_banner', "$name is too large!. Please select another Image");

                        return false;   

                        continue; // Skip large files

                    }

                    elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){

                        $this->form_validation->set_message('do_upload_banner', "$name is not a valid format!. Please select another Image");

                        return false;   

                        continue; // Skip invalid file formats
                    }

                    else

                    { // No error found! Move uploaded files 

                        if(move_uploaded_file($_FILES["userfile"]["tmp_name"][$field], $path.$name))

                        $count++; // Number of successfully uploaded file
                    }
                }
            }
        }
    }

I cannot figure out how to make it so when I insert the file name to the database that it matches the row id, of that insert.

I don't get it... Do you mean call the addBanner when a user upload a file??? Or update the data? Or else???

When image/file is selected eaither if it is only single or multiple image/file. I am trying to be able to get the name, of it insert into the correct location

Also each image/file name has its own link/url I am trying to get it so I can get the file name when both are inserted that the match each other.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.