I
I
ItsCoder2019-01-25 16:52:46
PHP
ItsCoder, 2019-01-25 16:52:46

opencart. Why is mysql_insert_id not returning an ID?

There is a standard Opencart function - adding clients.

addCustomer method
public function addCustomer($data) {
    if (isset($data['customer_group_id']) && is_array($this->config->get('config_customer_group_display')) && in_array($data['customer_group_id'], $this->config->get('config_customer_group_display'))) {
      $customer_group_id = $data['customer_group_id'];
    } else {
      $customer_group_id = $this->config->get('config_customer_group_id');
    }
    
    $this->load->model('account/customer_group');

    $customer_group_info = $this->model_account_customer_group->getCustomerGroup($customer_group_id);

    $this->db->query("INSERT INTO " . DB_PREFIX . "customer SET customer_group_id = '" . (int)$customer_group_id . "', store_id = '" . (int)$this->config->get('config_store_id') . "', language_id = '" . (int)$this->config->get('config_language_id') . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', custom_field = '" . $this->db->escape(isset($data['custom_field']['account']) ? json_encode($data['custom_field']['account']) : '') . "', salt = '" . $this->db->escape($salt = token(9)) . "', password = '" . $this->db->escape(sha1($salt . sha1($salt . sha1($data['password'])))) . "', newsletter = '" . (isset($data['newsletter']) ? (int)$data['newsletter'] : 0) . "', ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "', status = '" . (int)!$customer_group_info['approval'] . "', date_added = NOW()");

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

    if ($customer_group_info['approval']) {
      $this->db->query("INSERT INTO `" . DB_PREFIX . "customer_approval` SET customer_id = '" . (int)$customer_id . "', type = 'customer', date_added = NOW()");
    }
    
    return $customer_id;
  }


The getLastId function itself.
public function getLastId() {
  if ($this->connection) {
    return mysql_insert_id($this->connection);
  }
}

Returns int(1) in any case, but not the user ID. I've already broken my head, I can't understand what's the matter :)
I know that mysql_insert_id is outdated, but it should work. The site is PHP 5.5
Has anyone met with this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2019-01-25
@Rsa97

Is the ID field auto-incremental?
The db->query function does not make any more queries to the database?
What will the request return ? SELECT LAST_INSERT_ID()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question