Post by webiscore » Thu Mar 23, 2017 6:04 am

Hello, sorry if this is the wrong forum but it's under development so I posted. I'm developing a carousel extension for opencart that has Item ID, Item Name, Link, Image, Sort Order. The carousel had an issue while saving it will change the item id so I had to modify the code to get fixed ID queryed to the DB, however now I get the 1062 Error.
Notice: Error: Duplicate entry '1' for key 'PRIMARY'
Error No: 1062
INSERT INTO crousal SET crousal_id = '1', name = 'Baby & Toys', link = '/index.php?route=product/product&product_id=7570', image = 'data/carousel/banner2.jpg', sort_order = '0' in /home/user/public_html/system/database/mysql.php on line 49
DB Structure & Module info

Code: Select all

    $this->db->query("
        CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "mobiapp_crousal` (
          `crousal_id` INT(11) NOT NULL AUTO_INCREMENT,
          `name` VARCHAR(255) NOT NULL,
          `link` VARCHAR(255) NOT NULL,
          `image` VARCHAR(255) NOT NULL,
          `sort_order` INT(3) NOT NULL,
          PRIMARY KEY (`crousal_id`)
        ) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
Model File

Code: Select all

public function addCrousal($crousal_image) {
    $this->db->query("INSERT INTO " . DB_PREFIX . "mobiapp_crousal SET name = '" . $this->db->escape($crousal_image['name']) . "', sort_order = '" . (int)$crousal_image['sort_order'] . "'");

    $crousal_id = $this->db->getLastId();

    if (isset($crousal_image['crousal_image'])) {
        foreach ($crousal_image['crousal_image'] as $crousal_image) {
            $this->db->query("INSERT INTO " . DB_PREFIX . "mobiapp_crousal SET  crousal_id = '" . (int)$crousal_id . "', link = '" .  $this->db->escape($crousal_image['link']) . "', image = '" .  $this->db->escape($crousal_image['image']) . "'");

            $crousal_id = $this->db->getLastId();
        }
    }       
}   

public function editCrousal($crousal_image) {

    $crousal_id = $this->db->getLastId();

    $this->db->query("DELETE FROM " . DB_PREFIX . "mobiapp_crousal WHERE crousal_id = '" . (int)$crousal_id . "'");

    if (isset($crousal_image['crousal_image'])) {
        foreach ($crousal_image['crousal_image'] as $crousal_image) {

            $this->db->query("INSERT INTO " . DB_PREFIX . "mobiapp_crousal SET  crousal_id = '" . (int)$crousal_id . "', name = '" .  $this->db->escape($crousal_image['name']) . "', link = '" .  $this->db->escape($crousal_image['link']) . "', image = '" .  $this->db->escape($crousal_image['image']) . "', sort_order = '" . (int)$crousal_image['sort_order'] . "'");

            $crousal_id = $this->db->getLastId();

        }
    }
}

public function getCrousalImages() {
    $crousal_image_data = array();

    $crousal_image_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "mobiapp_crousal ORDER BY sort_order ASC");

    foreach ($crousal_image_query->rows as $crousal_image) {
        $crousal_image_description_data = array();

        $crousal_image_description_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "mobiapp_crousal WHERE crousal_id = '" . (int)$crousal_image['crousal_id'] . "'");

        $crousal_image_data[] = array(
            'crousal_image'            => $crousal_image,
            'link'                     => $crousal_image['link'],
            'name'                     => $crousal_image['name'],
            'crousal_id'               => $crousal_image['crousal_id'],
            'image'                    => $crousal_image['image'],
            'sort_order'               => $crousal_image['sort_order']
        );
    }

    return $crousal_image_data;
}
Any advice why I'm getting this error and how to resolve, thank you in advanced.

P.S.: Opencart 1.5.x

New member

Posts

Joined
Mon Jan 30, 2012 9:49 am
Who is online

Users browsing this forum: Google [Bot] and 199 guests