Skip to content

Duplicate entry when trying to add product with two level category neither existing #67

@brittainmark

Description

@brittainmark

Zen cart 1.5.8
Apache/2.4.41 (Ubuntu)
Database client version: libmysql - mysqlnd 8.0.20
PHP extension: mysqliDocumentation curlDocumentation mbstringDocumentation
PHP version: 8.0.20
Database Server version: 8.0.29-0ubuntu0.20.04.3 - (Ubuntu)

Trying to add new product with new two tier product categories.
"Ty Rhos Jewellery^Pendants"
the first category "Ty Rhos Jewellery" is added. The second category fails with duplicate entry

log file

MySQLi error 1062: Duplicate entry '65' for key 'categories.PRIMARY'
When executing:
INSERT INTO categories(categories_id, categories_image, parent_id, sort_order, date_added, last_modified
                ) VALUES (
                65, '', 65, 0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
                )
MySQLi error 1062: Duplicate entry '65-1' for key 'categories_description.PRIMARY'
When executing:
INSERT INTO categories_description SET
                    categories_id   = 65,
                    language_id     = 1,
                    categories_name = 'Pendants',
                    categories_description = ''
MySQLi error 1062: Duplicate entry '65' for key 'categories.PRIMARY'
When executing:
INSERT INTO categories(categories_id, categories_image, parent_id, sort_order, date_added, last_modified
                ) VALUES (
                65, '', 65, 0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
                )
MySQLi error 1062: Duplicate entry '65-1' for key 'categories_description.PRIMARY'
When executing:
INSERT INTO categories_description SET
                    categories_id   = 65,
                    language_id     = 1,
                    categories_name = 'Pendants',
                    categories_description = ''

the categories table has Next autoindex =65

This was used for the first category.

The issue appears to be that the Autoindex does not get incremented if you supply the index key.

Not sure if this is an issue with mysql.

I get a similar issue if I add two products having created the categories first.

for categories easypopulate_4_import.php 1087-1103

              $sql = "SHOW TABLE STATUS LIKE '" . TABLE_CATEGORIES . "'";
              $result = ep_4_query($sql);
              unset($sql);
              $row = $ep_4_fetch_array($result);
              unset($result);
              $max_category_id = $row['Auto_increment'];
              // if database is empty, start at 1
              if (!isset($max_category_id) || !is_numeric($max_category_id) || $max_category_id == 0) {
                $max_category_id = 1;
              }
              // TABLE_CATEGORIES has 1 entry per categories_id
              $sql = "INSERT INTO " . TABLE_CATEGORIES . "(categories_id, categories_image, parent_id, sort_order, date_added, last_modified
                ) VALUES (
                :categories_id:, '', :parent_id:, 0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
                )";
              $sql = $db->bindVars($sql, ':categories_id:', $max_category_id, 'integer');
              $sql = $db->bindVars($sql, ':parent_id:', $theparent_id, 'integer');

Could either add in check for max value
"SELECT max(categories_id) AS max_id FROM " . TABLE_CATEGORIES
and compare with $max_category_id and use the biggest.
Or
remove the categories_id from the insert and get the insert id from the insert query.
$current_category_id = ($ep_uses_mysqli ? mysqli_insert_id($db->link) : mysql_insert_id());

I don't know if this is an issue with my database? If it is, I don't know how to solve it.
The database was created using the default data from zc_install.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions