i'm new to coding, and i'm doing a car dealership website. I'm doing a car info form, with multiple selects and a image upload (ajax based got it on github). So i need to tie the images with the rest of the info about the car. My car info form has an auto increment id row in the database. So i'm trying to tie the images to the car by the cars id (i want every uploaded image to have the car id). I have this mysql insert code and i want to add the value of the last updated table id (from the car table) and add +1 to it. Is it a good idea? or should i go another way?

Should i add it in the ####### space in the code below (i already added carID row, and wrote it in the code)?

Database: login, table: masinos and the image table: files

protected function handle_file_upload($uploaded_file, $name, $size, $type, $error,
            $index = null, $content_range = null) {
        $file = parent::handle_file_upload(
            $uploaded_file, $name, $size, $type, $error, $index, $content_range
        );
        if (empty($file->error)) {
            $sql = 'INSERT INTO `'.$this->options['db_table']
                .'` (`carID`,`name`, `size`, `type`, `title`, `description`)'
                .' VALUES (#############,?, ?, ?, ?, ?)';
            $query = $this->db->prepare($sql);
            $query->bind_param(
                'sisss',
                $file->name,
                $file->size,
                $file->type,
                $file->title,
                $file->description
            );
            $query->execute();
            $file->id = $this->db->insert_id;
        }
        return $file;
    }
有帮助吗?

解决方案

You can create a random integer that is not in use to an car id and storage in $_SESSION, sou when you insert the car into the database, you can run an SQL UPDATE in the image files with the random integer stored into session upating to the real car id. I don't know if is the best method, but it's a solution.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top