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
Sharing the model/carousel.php editCarousel Function
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();
}
}
}
Any advice why I'm getting this error and how to resolve, thank you in advanced.
P.S.: Opencart 1.5.x
Edited: DB Structure & Module info
$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
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;
}
I did review for this module. Seems like this module has totally wrong structure. As you can see in method addCrousal foreach-statement with insertion to database. But in this insert carousal_id not incrementing so in the table will be records with not unique carousal_id. So this column can't be PRIMARY.
I suggest you to rewrite module with two tables "Slides" with relations (has many) "Images". Or you can remove PRIMARY index and operation will pass but it's not true solution.
Related
Im having some issues will my queries in my model. I have this edit function and inside that is a foreach that controls what gets updated and inserted where. the problem Im having is the last set of else statements that write to the communication table.
The records being written are tied to an overall campaign id. that id is stored to each record. So each record may have its own communication_id but they all would have the same campaign_id.
So currently setting the WHERE to campaign_id edits all records. I need to use the communication_id in this instance but how do I get it before the communication queries? Say a record has communication_id 50 I want to get that id and then use that in the WHERE. Im not sure how to do that though.
public function editCampaign($campaign_id, $data) {
$this->db->query("UPDATE " . DB_PREFIX . "campaigns SET campaign_name = '" . $this->db->escape($data['campaign_name']) . "', campaign_giving_goal = '" . (float)$data['campaign_giving_goal']
. "', code = '" . $this->db->escape($data['code']) . "', campaign_active = '" . $this->db->escape($data['campaign_active']) . "', campaign_giving_count_goal = '" . (float)$data['campaign_giving_count_goal'] . "', campaign_owner = '" . $this->db->escape($data['campaign_owner']). "'
, date_beginning = '" . $this->db->escape($data['date_beginning']). "', date_ending = '" . $this->db->escape($data['date_ending']). "' WHERE campaign_id = '" . (int)$campaign_id . "'");
$parent_id = 0;
$this->db->query("DELETE FROM " . DB_PREFIX . "campaign_components WHERE campaign_id = '" . (int)$campaign_id . "'");
//$this->db->query("DELETE FROM " . DB_PREFIX . "communication WHERE campaign_id = '" . (int)$campaign_id . "'");
foreach($data['component_module'] as $component_data) {
if ($component_data['component_type'] =='EVENT'){
if(isset($component_data['component_parent_id'])){
$parent_id = $component_data['component_parent_id'];
$this->db->query("UPDATE " . DB_PREFIX . "product SET model = '" . $this->db->escape($component_data['component_type']) . "', date_starting = '" . $this->db->escape($component_data['component_start_date']). "', date_ending = '" . $this->db->escape($data['date_ending']). "', date_added = NOW() WHERE product_id = '" . (int)$parent_id . "'");
$this->db->query("UPDATE " . DB_PREFIX . "product_description SET name = '" . $this->db->escape($component_data['component_name']) . "', language_id = '1' WHERE product_id ='" . (int)$parent_id . "'");
}else{
$this->db->query("INSERT INTO " . DB_PREFIX . "product SET model = '" . $this->db->escape($component_data['component_type']) . "', date_starting = '" . $this->db->escape($data['date_beginning']). "', date_ending = '" . $this->db->escape($data['date_ending']). "', date_added = NOW()");
$parent_id = $this->db->getLastId();
$this->db->query("INSERT INTO " . DB_PREFIX . "product_description SET name = '" . $this->db->escape($component_data['component_name']) . "', language_id = '1', product_id ='" . (int)$parent_id . "'");
$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_category SET category_id = '82', product_id ='" . (int)$parent_id . "' ");
}
}else{
$this->db->query("UPDATE " . DB_PREFIX . "communication SET subject = '" . $this->db->escape($component_data['component_name']) . "', channel = '" . $this->db->escape($component_data['component_type']) . "', status = '" . $this->db->escape($component_data['component_status']) . "'
, status_date = '" . $this->db->escape($component_data['component_start_date']). "', status = '" . $this->db->escape($component_data['component_status']) . "', created_by = '" . $this->db->escape($component_data['component_owner']) . "', date_added = NOW(), campaign_id = '" . (int)$campaign_id . "'");
}
$this->db->query("INSERT INTO " . DB_PREFIX . "campaign_components SET component_name = '" . $this->db->escape($component_data['component_name']) . "', component_type = '" . $this->db->escape($component_data['component_type']) . "', component_status = '" . $this->db->escape($component_data['component_status']) . "'
, component_owner = '" . $this->db->escape($component_data['component_owner']). "', component_start_date = '" . $this->db->escape($component_data['component_start_date']). "', campaign_id = '" . (int)$campaign_id . "', parent_id = '" . (int)$parent_id . "'");
}
$this->cache->delete('parent_id');
return $campaign_id;
}
Join with the communication table:
UPDATE campaigns AS ca
JOIN communication AS co ON ca.communication_id = co.communication_id
SET ca.col1 = val1, ca.col2 = val2, ...
WHERE co.campaign_id = $campaign_id
(I've left out all the PHP variables so you can see the general structure of the query.)
i want customer only add single item in her cart with any number of quantity. if cart already have item it's should be remove and newly added item add to cart in Opencart 2.x.
open system/library/cart.php. You will find follwoing function.
public function add
You can run delete query at very fast in this function.
public function add($product_id, $quantity = 1, $option = array(), $recurring_id = 0) {
$this->db->query("DELETE FROM " . DB_PREFIX . "cart");
$this->db->query("INSERT " . DB_PREFIX . "cart SET api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "', customer_id = '" . (int)$this->customer->getId() . "', session_id = '" . $this->db->escape($this->session->getId()) . "', product_id = '" . (int)$product_id . "', recurring_id = '" . (int)$recurring_id . "', `option` = '" . $this->db->escape(json_encode($option)) . "', quantity = '" . (int)$quantity . "', date_added = NOW()");
}
This is complete function.
I am using the opencart system and I've added a specific form for our company. Basically it keeps up with our orders as we input into the system. I'm trying to add to multiple records to a table, but it only will work for the first one.
$this->db->query("INSERT INTO " . DB_PREFIX . "customer_orders SET customer = '" . $data['customer'] . "', email = '" . $data['email'] . "', telephone = '" . $data['telephone'] . "', purchase_order = '" . $data['purchase_order'] . "', date_received = '" . $date . "', date_due = '" . $data['date_due'] . "', insurance = '" . $data['insurance'] . "'");
$customer_order = $this->db->getLastId();
foreach ($data['description'] as $description) {
$this->db->query("INSERT INTO " . DB_PREFIX . "customer_order_description SET customer_order_id = '" . $customer_order . "', description = '" . $description . "'");
}
This item would have 2 descriptions, so it should place each description in the order description table, but it only does the first one.
I need to change one of the links in the admin navigation menu, specifically the settings item to be transferred to the default store directly.
Can anyone help ?
I found the solution..
I changed the link in the admin\controller\template\common\menu
$data['setting'] = $this->url->link('setting/setting', 'token=' . $this->session->data['token'], 'SSL');
and changed the following in the admin\model\template\setting\setting
public function editStore($store_id= 0, $data) {
$this->event->trigger('pre.admin.store.edit', $data);
$this->db->query("UPDATE " . DB_PREFIX . "store SET name = '" . $this->db->escape($data['config_name']) . "', `url` = '" . $this->db->escape($data['config_url']) . "', `ssl` = '" . $this->db->escape($data['config_ssl']) . "' WHERE store_id = 0'"); // $this->db->query("UPDATE " . DB_PREFIX . "store SET name = '" . $this->db->escape($data['config_name']) . "', `url` = '" . $this->db->escape($data['config_url']) . "', `ssl` = '" . $this->db->escape($data['config_ssl']) . "' WHERE store_id = '" . (int)$store_id . "'");
$this->cache->delete('store');
$this->event->trigger('post.admin.store.edit', $store_id);
}
I am making a classified website in wordpress with classipress theme,
http://www.mohibsalahuddin.com/client/ads/test-ad/
for showing author is online or offline on ad page, i wrote the following code, but it does not work if two users registered at a time... If registered user wants to see another registered user's status it always shows offline...
I though may be i missed session start thing do i have to write it? Kindly help me in this
I write the code below
First I create the table in MYSQL
CREATE TABLE wordpress_users_online` (
user_id int(11) default NULL,
full_name varchar(64) NOT NULL default '',
session_id varchar(128) NOT NULL default '',
ip_address varchar(15) NOT NULL default '',
time_entry varchar(14) NOT NULL default '',
time_last_click varchar(14) NOT NULL default '',
last_page_url varchar(255) NOT NULL default ''
) ENGINE=MyISAM;`
THEN I WRITE THE FILE wordpress_users_online.php with following code
<?php
define('TABLE_USERS', 'wordpress_users');
define('TABLE_USER_ONLINE', 'wordpress_users_online');
function update_user_online() {
//global $user_id;
global $current_user;
get_currentuserinfo();
if ($current_user->ID && $current_user->user_login) {
$wo_user_id = $current_user->ID;
$wo_full_name = $user['user_nicename'];
} else {
$wo_user_id = '';
$wo_full_name = 'Guest';
}
$wo_session_id = session_id();
$wo_ip_address = getenv('REMOTE_ADDR');
$wo_last_page_url = getenv('REQUEST_URI');
$current_time = time();
$xx_mins_ago = ($current_time - 300);
// remove entries that have expired
mysql_query("delete from " . TABLE_USER_ONLINE . " where time_last_click < '" . $xx_mins_ago . "'");
$stored_user_query = mysql_query("select count(*) as count from " . TABLE_USER_ONLINE . " where session_id = '" . mysql_escape_string($wo_session_id) . "'");
$stored_user = mysql_fetch_array($stored_user_query);
if ($stored_user['count'] > 0) {
mysql_query("update " . TABLE_USER_ONLINE . " set user_id = '" . (int)$wo_user_id . "', full_name = '" . mysql_escape_string($wo_full_name) . "', ip_address = '" . mysql_escape_string($wo_ip_address) . "', time_last_click = '" . mysql_escape_string($current_time) . "', last_page_url = '" . mysql_escape_string($wo_last_page_url) . "' where session_id = '" . mysql_escape_string($wo_session_id) . "'");
} else {
mysql_query("insert into " . TABLE_USER_ONLINE . " (user_id, full_name, session_id, ip_address, time_entry, time_last_click, last_page_url) values ('" . (int)$wo_user_id . "', '" . mysql_escape_string($wo_full_name) . "', '" . mysql_escape_string($wo_session_id) . "', '" . mysql_escape_string($wo_ip_address) . "', '" . mysql_escape_string($current_time) . "', '" . mysql_escape_string($current_time) . "', '" . mysql_escape_string($wo_last_page_url) . "')");
}
}
function is_online($user_id) {
$stored_user_query = mysql_query("select user_id from ".TABLE_USER_ONLINE." where user_id = '" . mysql_escape_string($user_id) . "'");
$stored_user = mysql_fetch_array($stored_user_query);
if(isset($stored_user['user_id'])){
$online = '<img src="http://www.mohibsalahuddin.com/client/wp-content/themes/classipress-313/images/online.png" border="0" /> Online';
echo $online;
}
else {
$online = '<img src="http://www.mohibsalahuddin.com/client/wp-content/themes/classipress-313/images/offline.png" border="0" /> Offline';
echo $online;
}
return $online;
}
?>
There's actually a wordpress plugin for this already...
--Edit--
http://wordpress.org/extend/plugins/wp-useronline/
Sorry the following was the original suggestions but they were the wrong links:
http://wordpress.org/extend/plugins/who-is-online/faq/
http://wordpress.org/extend/plugins/wassup/
Remember, don't re-invent the wheel unless you're going to make it hover too.