mysql query selecting wrong field - php

$something_else = mysql_query('SELECT image_id FROM items p LEFT JOIN list up ON p.item_id = up.item_id WHERE up.UserID = "' . $user_id . '"');
while ($r=mysql_fetch_assoc($something_else)){
foreach($r as $item_id2)
$query ='DELETE FROM list WHERE UserID="' .$user_id. '" AND item_id="' .$item_id2. '"';}
This is for a product "wish list." Each is relative to a user. I can add to the wish list, but I can't delete the proper item. What this code is doing is deleting the last item on the list, or perhaps the item with the highest 'image_id'.
Either way, I'm not getting the relative 'image id' pertaining to the the associated item.
I'm pretty sure this should be enough information to solve the problem. I'm most certain my problem lies in the MySQL query:
mysql_query('SELECT image_id FROM items p LEFT JOIN list up ON p.item_id = up.item_id WHERE up.UserID = "' . $user_id . '"');

first of all as mentioned before your queries are NOT secure, please use PDO. anyways try this :
$something_else = mysql_query('SELECT image_id FROM items p LEFT JOIN list up ON p.item_id = up.item_id WHERE up.UserID = "' . $user_id . '"');
while ($r=mysql_fetch_assoc($something_else)) {
$query ='DELETE FROM list WHERE UserID="' .$user_id. '" AND item_id="' .$r['image_id'] . '"';
}

Related

SQL Join Issue PHP

I'm trying to do a SQL join to return records from one table for users who don't have a corresponding entry in a second table against the record in the first table. The query I'm trying is below, which I think should return what I want, but it's giving a syntax error:
$query = "SELECT * FROM `suppliers` LEFT OUTER JOIN `matches` ON suppliers.supplierid = matches.supplier_id WHERE '" . $_SESSION['userEmail'] . "' NOT IN matches.user";
Any ideas or suggestions? What I'm trying to achieve from the above is to only show results for which the logged in user hasn't marked the supplier record as viewed.
TIA
$query = "SELECT * FROM `suppliers` LEFT OUTER JOIN `matches` ON suppliers.supplierid = matches.supplier_id WHERE matches.user NOT IN '" . $_SESSION['userEmail'] . "'";
not in clause not added properly.
correct your where clause :
$query = "SELECT * FROM `suppliers` LEFT OUTER JOIN `matches` ON suppliers.supplierid = matches.supplier_id WHERE matches.user NOT IN ('" . $_SESSION['userEmail'] . "') ";
OR if it is single value then you can use != instead of not in
$query = "SELECT * FROM `suppliers` LEFT OUTER JOIN `matches` ON suppliers.supplierid = matches.supplier_id WHERE matches.user != '" . $_SESSION['userEmail'] . "' ";
You should swap your WHERE Conditions and trim with brackets ().
$query = "
SELECT * FROM `suppliers` LEFT OUTER JOIN `matches`
ON suppliers.supplierid = matches.supplier_id
WHERE matches.user NOT IN ('" . $_SESSION['userEmail'] . "')
";
Your logic is just not correct. You can use LEFT JOIN, but the query should look like:
SELECT s.*
FROM suppliers s LEFT OUTER JOIN
matches m
ON m.supplierid = s.supplier_id AND
m.user IN ('" . $_SESSION['userEmail'] . "')
WHERE m.supplierid IS NULL;
Note: This row may be causing a syntax error or unexpected results:
m.user IN ('" . $_SESSION['userEmail'] . "')
You will need to look at the syntax after variable substitution to figure that one out. This answer addresses the logic of the query, so it will do what you actually want when the syntax is fixed.

Opencart to enable autocomplete for name AND model

I am using OC 2.2.0. On frontend, I need autocomplete both on name and model. Right now, only name autocompletes. In catalog/model/catalog/product.php I changed
$sql .= " OR LCASE(p.model) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
to
$sql .= " OR LCASE(p.model) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
But still can't get autocomplete on model. Instead, I always must enter the whole model name, but it must be autocomplete. Any suggestions?
After many days of researching and looking at the code, I managed to understand and fix my problem. I was looking at all the wrong places. The ONLY file which needs changing is getdata.php which is in the root of your OC. So, default autocomplete is for the product name only. It is defined in this file like this:
$sql = "SELECT pd.name FROM " . DB_PREFIX . "product p," . DB_PREFIX . "product_description pd WHERE p.status = 1 AND p.product_id = pd.product_id AND language_id = '".$id."' AND UPPER(pd.name) like UPPER('%$q%') GROUP BY pd.product_id ORDER BY pd.name ASC";
$res = mysql_query($sql);
if(mysql_num_rows($res)>0){
while($ro = mysql_fetch_assoc($res)){
$name = str_replace( array( '\'', '"', ',' , ';', '<', '>','&quot','&'), ' ', $ro['name']);
//$str[]= $name."\n";
echo $name."\n";
}
}
Now, all you need to do is add the exact query for model or whichever database field in the oc_product table you need autocomplete for. Meaning, for example, copy the name query and replace pd.name with p.model and wherever you find ['name']replace with ['model']. And voila! I really hope this helps someone, as up until today I had trouble realizing this myself.

PHP fix Column 'id' in field list is ambiguous without change column name

I have this vode for show result using PHP join method:
$DB_QUERY = mySqli::f("SELECT name,id, " . AUTHOR . ".id,author_id,book_id FROM " . AUTHOR . " JOIN " . AUTHORS . " ON
" . AUTHOR . ".id = " . AUTHORS . ".author_id WHERE " . AUTHORS . ".book_id = ? ORDER BY name ASC LIMIT 8 ", $id);
foreach($DB_QUERY as $row){
echo $row['id'];
}
Now I see this :
Column 'id' in field list is ambiguous
I now this error when I have two column with name id. how do fix this error without change id name in one column?!
EDIT:
author table:
id|name|
authors table:
id|author_id|book_id
$DB_QUERY = mySqli::f("SELECT author.name, author.id as a_id,
authors.id as as_id, authors.author_id, authors.book_id
FROM author INNER JOIN authors ON author.id = authors.author_id
WHERE authors.book_id = ? ORDER BY name ASC LIMIT 8 ", $id);
For instance.
Then your field real names haven't changed but in your php you can use their temporary "nicknames" a_id and as_id.
Though I am not sure you need authors.id for anything... if it is only your PK on the table maybe you should drop it and use authors.author_id and authors.book_id as your PK... or.. if you are not bringing content from other tables with it... just don't mention it on your select.
You have selected id in your query without any alias & you are applying join on Author & Authors table. I think as both tables contains id column you are getting the error.
Try this
$DB_QUERY = mySqli::f("SELECT name, " . AUTHORS . ".id, " . AUTHOR . ".id,author_id,book_id FROM " . AUTHOR . " JOIN " . AUTHORS . " ON
" . AUTHOR . ".id = " . AUTHORS . ".author_id WHERE " . AUTHORS . ".book_id = ? ORDER BY name ASC LIMIT 8 ", $id);

Querying MySQL tables

I am working on someone else program. I have 2 tables Products and Seller. These tables are connected and the connection is made in the Seller table. I want to display the Product name from the Products table and also display the Seller who has made the product entry.
This is how I display the product table:
public function getProductName($product_id,$language_id) {
$query = $this->db->query("SELECT name FROM " . DB_PREFIX . "product_description WHERE
product_id = '".(int)$product_id."'
AND language_id = '".(int)$language_id."'");
if($query->row AND $query->row['name'] != '') {
return $query->row['name'];
} else {
return '-';
}
}
The Seller table has fields seller_name, seller_id, sproduct_id. How can I pull the associated seller name for each product?
Using your code:
public function getProductName($product_id, $language_id)
{
$strSQL = "SELECT p.name, s.seller_name FROM " . DB_PREFIX .
"product_description p INNER JOIN " . DB_PREFIX .
"Seller s ON p.product_id = s.sproduct_id WHERE product_id = '" .
intval($product_id) . "' AND language_id = '" .
intval($language_id) . "'";
$query = $this->db->query($strSQL);
if($query->row AND $query->row['name'] != '') {
return $query->row['name'];
} else {
return '-';
}
}
If product_id and language_id are integer values you don't need to enclosed them in ''. Also remember to add error checking to make sure that $product_id and $language_id have valid values for your query. More info on SQL joins.
You'll want to join the two tables together on the common product_id key. Here's the resultant Sql you'll want. Also, if product_id and language_id are integers in the database (as they seem to be in PHP) that you can drop the '':
"SELECT pd.name, s.seller_name
FROM " . DB_PREFIX . ".product_description pd
INNER JOIN " . DB_PREFIX . ".Seller s on pd.product_id = s.sproduct_id
WHERE
product_id = ".(int)$product_id."
AND language_id = ".(int)$language_id.";";

Opencart use Category as parent for store ( multi store system )

Hy all,
I'm working on an multi store website.
The main store is this. The second store ( same db, multi store ) is this with uid 4
I've got for the second store an categorie with multiple childs, with grand childs and so on...
My question is very simple, how can i make the categorie ( with uid 355 ) the parent categorie for that store instead of uid 0
I'll use VQMOD later, but i'm now inserting code directly in the files, for fast programming / debugging
The file that i'm changing is in: [root]/catalog/module/category.php
I've changed this code:
public function getCategories($parent_id = 0) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name)");
return $query->rows;
}
To this:
public function getCategories($parent_id = 0) {
if( $this->config->get('config_store_id') == 4 && $parent_id == 0 ){
$parent_id = 355;
echo "test1";
}
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name)");
return $query->rows;
}
And that kinda works. At the home page, the wrong categorie menu list is gone ( there's no menu anymore ), and at an categorie list page like this one there are the right categorie's on the left.
If i delete the && $parent_id == 0 part, it goes wrong at both the page's.
Does anybody got an clue of how to fix it? Any help would be apriciated. If you need more info, i will give it to you.
-ps- sorry for my bad english
Alright, after a lot of try's i searched the fora's of OpenCart better and came upon this thread:
OpenCart Fora Thread
The anser there is as follows:
There's a "top" flag you set to make it show up as a top level menu
So what i did is this:
I've edited the new main category ( with uid 355 ) and flagged it as top, that didn't helped, but i left it on.
Then i've edited all the direct childs of the main category and flagged them as top and foila, there they are. the problem was solved.
So with my edit to the main core, and the flagging of the categorie's, the problem is now solved

Categories