M
M
Muvka2019-07-13 10:58:24
opencart
Muvka, 2019-07-13 10:58:24

Can the database prohibit changing some table values ​​when transferring the database?

Such a problem. After the transfer, when editing a product in OpenCart, these errors come out

Fatal error: Uncaught Exception: Error: Duplicate entry '240' for key 'PRIMARY'<br />Error No: 1062<br />INSERT INTO oc_product_option SET product_option_id = '240', product_id = '62', option_id = '14', required = '1'

in different tables. In this case, the code explicitly first clears the table from the desired value
$this->db->query("DELETE FROM " . DB_PREFIX . "product_option WHERE product_id = '" . (int)$product_id . "'");
    $this->db->query("DELETE FROM " . DB_PREFIX . "product_option_value WHERE product_id = '" . (int)$product_id . "'");

    if (isset($data['product_option'])) {
      foreach ($data['product_option'] as $product_option) {
        if ($product_option['type'] == 'select' || $product_option['type'] == 'radio' || $product_option['type'] == 'checkbox' || $product_option['type'] == 'image') {
          if (isset($product_option['product_option_value'])) {
            $this->db->query("INSERT INTO " . DB_PREFIX . "product_option SET product_option_id = '" . (int)$product_option['product_option_id'] . "', product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', required = '" . (int)$product_option['required'] . "'");

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

            foreach ($product_option['product_option_value'] as $product_option_value) {
              
        $this->db->query("INSERT INTO " . DB_PREFIX . "product_option_value SET product_option_value_id = '" . (int)$product_option_value['product_option_value_id'] . "', product_option_id = '" . (int)$product_option_id . "', product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', option_value_id = '" . (int)$product_option_value['option_value_id'] . "', quantity = '" . (int)$product_option_value['quantity'] . "', option_default = '" . (int)$product_option_value['option_default'] . "', subtract = '" . (int)$product_option_value['subtract'] . "', price = '" . (float)$product_option_value['price'] . "', price_prefix = '" . $this->db->escape($product_option_value['price_prefix']) . "', points = '" . (int)$product_option_value['points'] . "', points_prefix = '" . $this->db->escape($product_option_value['points_prefix']) . "', weight = '" . (float)$product_option_value['weight'] . "', weight_prefix = '" . $this->db->escape($product_option_value['weight_prefix']) . "'");
      
            }
          }
        } else {
          $this->db->query("INSERT INTO " . DB_PREFIX . "product_option SET product_option_id = '" . (int)$product_option['product_option_id'] . "', product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', value = '" . $this->db->escape($product_option['value']) . "', required = '" . (int)$product_option['required'] . "'");
        }
      }
    }

But the error does not always occur, but very often.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Muvka, 2019-07-13
@Muvka

Understood. The problem was that the form was submitted 2 times.

A
Alexander, 2019-07-13
@NeiroNx

Flew key sequences - the keys that are generated during insertion are not always correct.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question