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.
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.)
$queryb = "SELECT product, imei, country, warranty, config from PRODUCT WHERE product_slno = '$mnserialno' ";
$resultb = mysql_query($queryb, $gndbconn) ;
if(mysql_num_rows($resultb) > 0)
{
$queryc = "UPDATE PRODUCT SET product='$desc', product_slno='$mnserialno',imei='$imei',country='$country',warranty='$warranty',config='$config' WHERE product_slno = '.$mnserialno.' ";
$resutc = mysql_query($queryc, $gndbconn) ;
}
else{
$querya = "INSERT INTO PRODUCT SET product='$desc', product_slno='$mnserialno',imei='$imei',country='$country',warranty='$warranty',config='$config'";
$resulta = mysql_query($querya, $gndbconn) ;
}
I want to check the serial number if that serial number already exist in database so records get update, otherwise it get insert into the database.
but the code inserting the records only, no updation, what is the fault i am not getting,
how to prevent the duplicate entry?
INSERT INTO PRODUCT SET
(`product`, `product_slno`, `imei`, `country`, `warranty`, `config`)
VALUES
('" . $desc . "', '" . $mnserialno . "', '" . $imei . "', '" . $country . "', '" . $warranty . "', '" . $config . "')
ON DUPLICATE KEY UPDATE
product='" . $desc . "',
product_slno='" . $mnserialno . "',
imei='" . $imei . "',
country='" . $country . "',
warranty='" . $warranty . "',
config='" . $config . "'";
Hey Guys I am trying to pass data to my model, but for some reason I keep getting an "undefined customitem_id" in my model file. I am testing to see if it will even send to the model so:
the code is as follows. My controller file from customer.php file
$data['customitem_id']= 19;
if(isset($this->request->post['customitem_id'])) {
$this->request->post['customitem_id'];
}
My code from:
public function editCustomer($customer_id, $data) {
if (!isset($data['custom_field'])) {
$data['custom_field'] = array();
}
$this->db->query("UPDATE " . DB_PREFIX . "customer SET customer_group_id = '" . (int)$data['customer_group_id'] . "', sales_representative = '" . $this->db->escape($data['username']) . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "', custom_field = '" . $this->db->escape(isset($data['custom_field']) ? serialize($data['custom_field']) : '') . "', newsletter = '" . (int)$data['newsletter'] . "', status = '" . (int)$data['status'] . "', approved = '" . (int)$data['approved'] . "', safe = '" . (int)$data['safe'] . "' WHERE customer_id = '" . (int)$customer_id . "'");
$this->db->query("UPDATE " . DB_PREFIX . "custom_item SET customer_id = '" . (int)$customer_id . "' WHERE customitem_id = '" . (int)$customitem_id . "'");
it keeps giving me an undefined variable in the model file. How would I go about making sure it sends the data?
Thanks for your help.
Looks like you're passing an array $data to your editCustomer($customer_id, $data) right?
Try changing this from:
$this->db->query("UPDATE " . DB_PREFIX . "custom_item SET customer_id = '" . (int)$customer_id . "' WHERE customitem_id = '" . (int)$customitem_id . "'")
to
$this->db->query("UPDATE " . DB_PREFIX . "custom_item SET customer_id = '" . (int)$customer_id . "' WHERE customitem_id = '" . (int)$data['customitem_id'] . "'")
note that I changed (int)$customitem_id to (int)$data['customitem_id']
You're using variable $customitem_id in your model but in your controller it is $data['customitem_id']. You likely just need to change $customitem_id to $data['customitem_id'] in your model.
Attempting to push new product prices to past orders. Essentially, when I edit the product price it needs to update in all orders containing that price Here's the code:
public function editProduct($product_id, $data) {
$this->event->trigger('pre.admin.product.edit', $data);
$this->db->query("UPDATE " . DB_PREFIX . "product SET model = '" . $this->db->escape($data['model']) . "', sku = '" . $this->db->escape($data['sku']) . "', upc = '" . $this->db->escape($data['upc']) . "', ean = '" . $this->db->escape($data['ean']) . "', jan = '" . $this->db->escape($data['jan']) . "', isbn = '" . $this->db->escape($data['isbn']) . "', mpn = '" . $this->db->escape($data['mpn']) . "', location = '" . $this->db->escape($data['location']) . "', quantity = '" . (int)$data['quantity'] . "', minimum = '" . (int)$data['minimum'] . "', subtract = '" . (int)$data['subtract'] . "', stock_status_id = '" . (int)$data['stock_status_id'] . "', date_available = '" . $this->db->escape($data['date_available']) . "', manufacturer_id = '" . (int)$data['manufacturer_id'] . "', shipping = '" . (int)$data['shipping'] . "', price = '" . (float)$data['price'] . "', points = '" . (int)$data['points'] . "', weight = '" . (float)$data['weight'] . "', weight_class_id = '" . (int)$data['weight_class_id'] . "', length = '" . (float)$data['length'] . "', width = '" . (float)$data['width'] . "', height = '" . (float)$data['height'] . "', length_class_id = '" . (int)$data['length_class_id'] . "', status = '" . (int)$data['status'] . "', tax_class_id = '" . (int)$data['tax_class_id'] . "', sort_order = '" . (int)$data['sort_order'] . "', date_modified = NOW() WHERE product_id = '" . (int)$product_id . "'");
//Push Product Edits //
$product_qry = $this->db->query("SELECT price FROM " . DB_PREFIX . "product WHERE product_id = '" . (int)$product_id . "'");
$new_price = $product_qry->row['price'];
$order_product_id_qry = $this->db->query("SELECT order_product_id FROM " . DB_PREFIX . "order_product");
$order_product_id = $order_product_id_qry->row['order_product_id'];
$order_qry = $this->db->query("SELECT quantity FROM " . DB_PREFIX . "order_product WHERE order_product_id = '" . (int)$order_product_id . "'");
$product_quantity = $order_qry->row['quantity'];
$new_total = $product_quantity * $new_price;
$this->db->query("UPDATE " . DB_PREFIX . "order_product SET price = '" . $new_price . "' WHERE product_id = '" . (int)$product_id ."'");
$this->db->query("UPDATE " . DB_PREFIX . "order_product SET total = '" . $new_total . "' WHERE order_product_id = '" . (int)$order_product_id . "'");
Updating the price is working great. I am no longer receiving an errors, but the order total is not updating. Any idea why?
I added a MYSQL Trigger onto the DB
CREATE TRIGGER `after_product_update` AFTER UPDATE ON `oc_product` FOR EACH ROW
BEGIN
UPDATE `order_product`
SET `price` = NEW.`price`, `total` = `quantity` * NEW.`price` WHERE `product_id` = NEW.`product_id`;
END
Do note other triggers have to be added to the tables oc_order_total in order to update the Subtotal and Total fields there.
I have a website in opencart the order id is jumping up.
means if a person has order something on website and his order id is 49 the order id of the next order should be 50 but it is shown as 51 or 52.
When I check in the database table oc_order it shown the same order for the missing order no.
class ModelCheckoutOrder extends Model {
public function addOrder($data) {
$this->db->query("INSERT INTO `" . DB_PREFIX . "order` SET invoice_prefix = '" . $this->db->escape($data['invoice_prefix']) . "', store_id = '" . (int)$data['store_id'] . "', store_name = '" . $this->db->escape($data['store_name']) . "', store_url = '" . $this->db->escape($data['store_url']) . "', customer_id = '" . (int)$data['customer_id'] . "', customer_group_id = '" . (int)$data['customer_group_id'] . "', emp_name = '" . $this->db->escape($data['emp_name']) . "', emp_ID = '" . $this->db->escape($data['emp_ID']) . "', email = '" . $this->db->escape($data['email']) . "', mobile_no = '" . $this->db->escape($data['mobile_no']) . "', fax = '" . $this->db->escape($data['fax']) . "', payment_emp_name = '" . $this->db->escape($data['payment_emp_name']) . "', payment_emp_ID = '" . $this->db->escape($data['payment_emp_ID']) . "', payment_company = '" . $this->db->escape($data['payment_company']) . "', payment_company_id = '" . $this->db->escape($data['payment_company_id']) . "', payment_tax_id = '" . $this->db->escape($data['payment_tax_id']) . "', payment_address_1 = '" . $this->db->escape($data['payment_address_1']) . "', payment_address_2 = '" . $this->db->escape($data['payment_address_2']) . "', payment_city = '" . $this->db->escape($data['payment_city']) . "', payment_postcode = '" . $this->db->escape($data['payment_postcode']) . "', payment_country = '" . $this->db->escape($data['payment_country']) . "', payment_country_id = '" . (int)$data['payment_country_id'] . "', payment_zone = '" . $this->db->escape($data['payment_zone']) . "', payment_zone_id = '" . (int)$data['payment_zone_id'] . "', payment_address_format = '" . $this->db->escape($data['payment_address_format']) . "', payment_method = '" . $this->db->escape($data['payment_method']) . "', payment_code = '" . $this->db->escape($data['payment_code']) . "', shipping_emp_name = '" . $this->db->escape($data['shipping_emp_name']) . "', shipping_emp_ID = '" . $this->db->escape($data['shipping_emp_ID']) . "', shipping_company = '" . $this->db->escape($data['shipping_company']) . "', shipping_address_1 = '" . $this->db->escape($data['shipping_address_1']) . "', shipping_address_2 = '" . $this->db->escape($data['shipping_address_2']) . "', shipping_city = '" . $this->db->escape($data['shipping_city']) . "', shipping_postcode = '" . $this->db->escape($data['shipping_postcode']) . "', shipping_country = '" . $this->db->escape($data['shipping_country']) . "', shipping_country_id = '" . (int)$data['shipping_country_id'] . "', shipping_zone = '" . $this->db->escape($data['shipping_zone']) . "', shipping_zone_id = '" . (int)$data['shipping_zone_id'] . "', shipping_address_format = '" . $this->db->escape($data['shipping_address_format']) . "', shipping_method = '" . $this->db->escape($data['shipping_method']) . "', shipping_code = '" . $this->db->escape($data['shipping_code']) . "', comment = '" . $this->db->escape($data['comment']) . "', total = '" . (float)$data['total'] . "', affiliate_id = '" . (int)$data['affiliate_id'] . "', commission = '" . (float)$data['commission'] . "', language_id = '" . (int)$data['language_id'] . "', currency_id = '" . (int)$data['currency_id'] . "', currency_code = '" . $this->db->escape($data['currency_code']) . "', currency_value = '" . (float)$data['currency_value'] . "', ip = '" . $this->db->escape($data['ip']) . "', forwarded_ip = '" . $this->db->escape($data['forwarded_ip']) . "', user_agent = '" . $this->db->escape($data['user_agent']) . "', accept_language = '" . $this->db->escape($data['accept_language']) . "', date_added = NOW(), date_modified = NOW()");
$this->db->query("INSERT INTO " . DB_PREFIX . "order_history SET order_id = '" . (int)$order_id . "', order_status_id = '" . (int)$order_status_id . "', notify = '1', comment = '" . $this->db->escape(($comment && $notify) ? $comment : '') . "', date_added = NOW()");
$order_product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");
foreach ($order_product_query->rows as $order_product) {
$this->db->query("UPDATE " . DB_PREFIX . "product SET quantity = (quantity - " . (int)$order_product['quantity'] . ") WHERE product_id = '" . (int)$order_product['product_id'] . "' AND subtract = '1'");
$order_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . (int)$order_product['order_product_id'] . "'");
foreach ($order_option_query->rows as $option) {
$this->db->query("UPDATE " . DB_PREFIX . "product_option_value SET quantity = (quantity - " . (int)$order_product['quantity'] . ") WHERE product_option_value_id = '" . (int)$option['product_option_value_id'] . "' AND subtract = '1'");
}
}
this is because there is a difference between what you see in the database itself and what you see in the orders admin page. and this difference is due to the fact that opencart stores something called a missing order id, and that happens for example one someone fills all the details and then by mistake he just reloads the checkout page and then he just press the "confirm" button. at this case for example in the orders admin page he booked two orders number and you will only see the second number - ofc the number of order ids skipped is the same as the number of missing orders stored by opencart itself.
if you want to test this go to checkout page and fill all orders and then confirm it, at this case you wont see any difference, however if you go to checkout page fill all info and then reload the page and then just press confirm you will see that the order id will skip one number.
in opencart database you wont see missing order ids, because opencart insert an order in the database only if it was confirmed.