combining mysql queries in a function php - php

I am trying to create function that archives a record. I have a list of records with each record having a dropdown with options delete, edit, archive. When archive is clicked Id like that record tp be moved to a new table and then that original record to be removed. Can I have an Insert and DElete run in the same query? If not how else can I achieve this? here is what I've currently got:
public function archiveCampaign($campaign_id) {
$this->db->query("INSERT INTO `" . DB_PREFIX . "campaigns_archive` SELECT * WHERE campaign_id = '" . (int)$campaign_id . "'");
$this->db->query("DELETE FROM `" . DB_PREFIX . "campaigns` WHERE campaign_id = '" . (int)$campaign_id . "'");
return $campaign_id;
}

You're already close
Sql doesn't allow multiple commands in a single statement, with the exception of subqueries. Only SELECT statements have an operable return value, thus they are the only commands that can be used in a subquery. That means you will have to run two commands very similar to what you already have written.
If I assume your db object's query method returns true/false, or something to evaluate (it probably does). Return success/fail, or throw an exception somewhere, so you don't wind up with corrupt data.
public function archiveCampaign($campaign_id) {
$insertResult = $this->db->query("INSERT INTO `" . DB_PREFIX . "campaigns_archive` SELECT * FROM `" . DB_PREFIX . "campaigns` WHERE campaign_id = '" . (int)$campaign_id . "'");
if (!$insertResult) {
// DON'T DELETE THE DATA, THE INSERT FAILED!
return false; // or an error object, or throw an exception
}
$deleteResult = $this->db->query("DELETE FROM `" . DB_PREFIX . "campaigns` WHERE campaign_id = '" . (int)$campaign_id . "'");
if (!$deleteResult) {
// DELETE FAILED, remove from archive.
$this->db->query("DELETE FROM `" . DB_PREFIX . "campaigns_archive` WHERE campaign_id = '" . (int)$campaign_id . "'");
return false; // or an error object, or throw an exception
}
return true;
}
PHP PDO
Please consider using PHP:PDO.

Related

Return query with missing/different data only

i have a form where you can select category and will return products that are in this category
$query = $this->db->query("SELECT id, name FROM " . DB_PREFIX . "shared_products_product WHERE category = '" . (int)$id . "' AND status != 2");
Result
Array
(
[0] => Test 1
[1] => Test 2
[2] => Test 3
)
Than you can select only Test 1 and Test 2 to insert in different table
$this->db->query("INSERT INTO " . DB_PREFIX . "shared_products_view SET product_id = '" . (int)$product_id . "', shared_product_id = '" . (int)$shared_product_id . "', category_id = '" . (int)$cat_id . "'");
When i run 1st query how will i get result that still not inserted into shared_products_view for current category_id ?
you need to join table and check if value is already existing
Like this
$query = $this->db->query("SELECT `spp`.id, `spp`.name
FROM " . DB_PREFIX . "shared_products_product `spp`
LEFT JOIN " . DB_PREFIX . "shared_products_view `spv` ON `spp`.`id`=`spv`.shared_product_id
WHERE category = '" . (int)$id . "' AND status != 2 AND `spv`.shared_product_id IS NULL");
When you run your second query, the INSERT, the row will be added, unless you have an error. One thing that comes to mind is you seem to insert integers as strings.
It is better to use PDO and bindValue() and explicitly state it is an integer.
If I understand you right, you want to know if the INSERT succeeded. (Is that correct?)
In that case check the result of your command $this->db->query("INSERT ...").
You didn't mention what database handle $this->db is, but I expect it will return false on failure.
I highly recommend you study this hands-on guide first before proceeding:
https://phpdelusions.net/pdo

Read value of product's custom options in custom script of Opencart 2.x

Is is possible to read variable value of custom option using key value pair?
I am trying to read option value for a product in custom function.I am able to read value by referring it with array index like below
$option = array_filter($this->request->post['option']);
$product_serial_no = $option['93'] ;
I get magic array index $option[93] by reading variable value in Neatbean debug mode.
But option index is changing for different products thus I want to read value of variable 'Serial Number' using something like associative array.
Is it possible?
The options on product page look like below image.
when you are viewing the product page and say you select some of the options and click "add to cart" the post will carry this array
$_POST['option'] = array(93 => '22222') (as you have presented in your example)
the 93 is the $product_option_id and it allows you to pull all the information you need with this request:
$product_id = $this->request->post['product_id'];
foreach ($this->request->post['option']) as $product_option_id => $value) {
$option_query = $this->db->query("SELECT
po.product_option_id,
po.option_id,
od.name,
o.type
FROM " . DB_PREFIX . "product_option po
LEFT JOIN `" . DB_PREFIX . "option` o ON (po.option_id = o.option_id)
LEFT JOIN " . DB_PREFIX . "option_description od ON (o.option_id = od.option_id)
WHERE po.product_option_id = '" . (int)$product_option_id . "'
AND po.product_id = '" . (int)$product_id . "'
AND od.language_id = '" . (int)$this->config->get('config_language_id') . "'");
print_r($option_query->row);
}
This info should be sufficient enough to do any manipulations with options.
if the option has type of select or radio you may want to query also the possible variations like this
//... put this inside the foreach loop, listed above right after print_r($option_query->row);
if ($option_query->row['type'] == 'select' || $option_query->row['type'] == 'radio') {
$option_value_query = $this->db->query("SELECT
pov.option_value_id,
ovd.name,
pov.quantity,
pov.subtract,
pov.price,
pov.price_prefix,
pov.points,
pov.points_prefix,
pov.weight,
pov.weight_prefix
FROM " . DB_PREFIX . "product_option_value pov
LEFT JOIN " . DB_PREFIX . "option_value ov ON (pov.option_value_id = ov.option_value_id)
LEFT JOIN " . DB_PREFIX . "option_value_description ovd ON (ov.option_value_id = ovd.option_value_id)
WHERE pov.product_option_value_id = '" . (int)$value . "'
AND pov.product_option_id = '" . (int)$product_option_id . "'
AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
print_r($option_value_query->row);
}
And in case you have a checkbox (like in the example above)
//...also place this in the loop foreach in the code above.
if ($option_query->row['type'] == 'checkbox' && is_array($value)){
foreach ($value as $product_option_value_id) {
$option_value_query = $this->db->query("SELECT
pov.option_value_id,
pov.quantity,
pov.subtract,
pov.price,
pov.price_prefix,
pov.points,
pov.points_prefix,
pov.weight,
pov.weight_prefix,
ovd.name
FROM " . DB_PREFIX . "product_option_value pov
LEFT JOIN " . DB_PREFIX . "option_value_description ovd ON (pov.option_value_id = ovd.option_value_id)
WHERE pov.product_option_value_id = '" . (int)$product_option_value_id . "'
AND pov.product_option_id = '" . (int)$product_option_id . "'
AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
print_r($option_value_query->row);
}
}
You can view this exact code in system/library/cart/cart.php
hope this helps.

OpenCart, display the total number of products

I'm using OpenCart ecommerce, and I would like to see in the index of ecommerce the total number of products
I do not have (for now) access to the db opencart, so to understand what is the structure of the db I have referred to this image
And this is an example of a query that I'm trying to use to display the total number of products (the result is obviously not what I expect)
//Test Count Product
//$query_test = $db->query("SELECT " . DB_PREFIX . "product_description.name FROM " . DB_PREFIX . "product INNER JOIN " . DB_PREFIX . "product_description ON " . DB_PREFIX . "product.product_id = " . DB_PREFIX . "product_description.product_id");
$query_test = $db->query("SELECT * FROM " . DB_PREFIX . "product");
$count_test = 0;
foreach ($query_test as $row) {
$count_test++;
}
echo $count_test;
Try this:
$query = $db->query("SELECT COUNT(*) AS total FROM ".DB_PREFIX."product");
echo $query->row['total'];
Nicolo,
Your code is also correct. For that you need to use this code
$query_test = $db->query("SELECT * FROM " . DB_PREFIX . "product");
$count_test = 0;
foreach ($query_test->rows as $row) {
$count_test++;
}
echo $count_test;
But, I will recommend Monkeyman's way to get product_totals but with some modification (Monkeyman's code will NOT work for multiple stores)
$query = $db->query("SELECT COUNT(*) AS total FROM ".DB_PREFIX."product_to_store WHERE store_id = '" . (int)$this->config->get('config_store_id') . "'");
echo $query->row['total'];

opencart seo_url not working for categories

$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($part) . "'");
echo "SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($part) . "'";
exit();
i have added the keyword for the category and the output for this echo is
SELECT * FROM oc_url_alias WHERE keyword = 'fashion'
i echoed this part in seo_url.php under controller/common.
this is working for products and information but not for category even when i run this output query in sql it gives me a single row with keyqord ='fashion'
how can the query be right, yet it gives 0 when keyword is for category.

Using mysql_fetch_array in INSERT statement

I am trying to use the result of one mysql query in another mysql query, but I'm obviously doing something wrong. This is what I have:
<?php
$result = mysql_query('SELECT panel_product_no
FROM panelProduct
WHERE length_mm = "' . ($_POST["p_length_mm"]) . '"
AND width_mm = "' . ($_POST["p_width_mm"]) . '"
AND veneer_type = "' . ($_POST["p_veneer"]) . '"
AND lipping = "' . ($_POST["p_lipping"]) . '"');
$panel = mysql_fetch_array($result);
?>
And then I want to use that in this bit:
<?php
if(!empty($_POST[p_length_mm]) && !empty($_POST[p_width_mm]) && !empty($_POST[p_aperture]))
{
$sql3="INSERT INTO estimateDescribesPanelProduct (estimate_no, panel_product_no, quantity)
VALUES ('$_GET[estimate_no]','$panel','$_POST[p_quantity]')";
if (!mysql_query($sql3,$con))
{
die('Error: ' . mysql_error());
}
}
?>
The query is basically working in that it is inserting the posted estimate_no and quantity into the DB, but not the correct panel_product_no (it just inserts '0'). How can I get it to insert the $result value?
P.S. I know that I should not be using mysql functions and I will not be in future, however I am so nearly finished with this project that at this point I am not in a position change.
Your are basicly copying content from one table to another.
Wy not use the MySQL INSERT .. SELECT syntax?
as #Dmitry Makovetskiyd wrote, mysql_fetch_array() returns a resource, not manipulatable results.
For example:
$result = mysql_query('SELECT panel_product_no
FROM panelProduct
WHERE length_mm = "' . ($_POST["p_length_mm"]) . '"
AND width_mm = "' . ($_POST["p_width_mm"]) . '"
AND veneer_type = "' . ($_POST["p_veneer"]) . '"
AND lipping = "' . ($_POST["p_lipping"]) . '"');
$resource = mysql_fetch_object($result);
You need to add in:
$panel = $resource->'panel_product_no';
You can then continue with your second query.
Note the change from mysql_fetch_array() to mysql_fetch_object() - as your query suggests you are only retrieving a singular value from the table (assuming there is only a singular panel with the specified length, width, veneer type and lipping), the object method will work fine.

Categories