Finding and changing value in table - php

I have a table with images in SQL database.
It looks something like this.
SELECT * FROM eshop_images WHERE fg_idProduct='$id' AND main='1'
+----------+-----+------+--------------+
| idImages | url | main | fg_idProduct |
+----------+-----+------+--------------+
| 1 | x | 0 | 1 |
| 2 | x | 1 | 1 |
| 3 | x | 0 | 1 |
| 4 | x | 0 | 2 |
| 5 | x | 0 | 2 |
| 6 | x | 0 | 2 |
| 7 | x | 1 | 2 |
+----------+-----+------+--------------+
Each product can only have one main image, but what if I want to set some other image which is 0 as main one?
I have to go through the whole table where fg_idProduct=$id and SET them to null. I don't know how to do this, can somebody help me?
Thanks.

Try this:
UPDATE eshop_images
SET main = 0
WHERE main = 1
AND fg_idProduct = $id

UPDATE eshop_images
SET main = 0
WHERE main = 1
AND fg_idProduct = $id

Related

Reorder rows within groups

I am making a management system for a site that has a bunch of image galleries. The interface will allow the user to add, delete, or reorder images within each gallery.
I have a table for all the images across all galleries together, with 'id' auto-incremented, and default for sort_order set to 0.
+----+-----------+------------+------------+
| id |gallery_id | sort_order |
+----+-----------+------------+------------+
| 1 | 1 | 1 |
| 2 | 1 | 0 |
| 3 | 1 | 2 |
| 4 | 1 | 3 |
| 5 | 1 | 4 |
| 6 | 1 | 5 |
| 7 | 1 | 6 |
| 8 | 2 | 0 |
| 9 | 2 | 1 |
| 10 | 2 | 2 |
| 11 | 2 | 3 |
| 12 | 2 | 4 |
| 13 | 2 | 5 |
| 14 | 2 | 6 |
+----+-----------+------------+------------+
Here is the reorder query, using a serialized array via ajax:
if($_POST['item']) {
$order = 0;
foreach ($_POST['item'] as $id) {
$rearrange = $db->query
("UPDATE images SET sort_order = '".$order."' WHERE id = '".$id."'");
$order++;
It limits sort_order according to the length of the array that is passed, and then defaults to 0 for the next group its comes to. I can reorder, add, delete, and it always restricts changes to the gallery_id group I am editing.
(As an experiment, I inserted sort_order values ascending across all galleries, like this:
+----+-----------+------------+------------+
| id |gallery_id | sort_order |
+----+-----------+------------+------------+
| 1 | 1 | 0 |
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 1 | 4 |
| 5 | 1 | 5 |
| 6 | 1 | 6 |
| 7 | 1 | 7 |
| 8 | 2 | 8 |
| 9 | 2 | 9 |
| 10 | 2 | 10 |
| 11 | 2 | 11 |
| 12 | 2 | 12 |
| 13 | 2 | 13 |
| 14 | 2 | 14 |
+----+-----------+------------+------------+
And when I reordered rows using my interface, it limited the sort_order within gallery_id groups, exactly as shown in the first table).
I'm looking for caveats to this approach. Anyone done it this way?
UPDATE images
SET
sort_order = FIELD(id, A, B, C, D, E)
WHERE gallery_id = N
AND id IN(A, B, C, D, E)
I do it the same way, but I actually put the image inserted last by calculating the number of rows in each gallery. When a user wants to order he has to filter the list for a specific gallery first then he can sort the images in the gallery.
I cannot but point out the terrible security issues with your code. I really, really, really hope that you are not actually writing the queries like that as anybody can easily do whatever they want with your database.
http://xkcd.com/327/ comes to mind. If I submit an item like 0'; drop table images; your entire table is gone.

PHP Find ID in more than one mysql table

I have a problem and don't find a way to solve it.
I have this mySQL Table:
product_insert
productInsertID | productID | .........
1 | 1
2 | 1
3 | 2
4 | 3
product_insert_options
productInsertID | optionsCategory | optionsValue
1 | 1 | Option1
1 | 2 | Option2
1 | 1 | More Option1
1 | 2 | More Option2
Now I have this php Variables
$productID = 1
$productOptionArray
[0][optionsCategory] = 1
[0][optionsValue] = More Option1
[1][optionsCategory] = 2
[1][optionsValue] = More Option2
Now I search the productInsertID for my php Variables. In this sample it's the ID 1. I can have more than 2 options, and one productInsertID can have more than one inserts.
How can I make it?
Sorry for my bad english.
EDIT (from answer):
It is a little bit difficult. I try with a sample shop cart and hope you now what I mean:
We have a shop with product and product attribute. Now we have this table: shop_cart
cartID | productID | userID | ...
1 | 1 | 1
2 | 2 | 1
3 | 1 | 2
4 | 1 | 1
Now we have the attribute. product_attribute
attributeID | productID | attribute
1 | 1 | Color
2 | 1 | Name
And we have the Table shop_cart_attribute
ID | cartID | attributeID | productID | value
1 | 1 | 1 | 1 | red
2 | 1 | 2 | 1 | Tina
3 | 3 | 1 | 1 | blue
4 | 3 | 2 | 1 | Tina
5 | 4 | 1 | 1 | blue
6 | 4 | 2 | 1 | Nina
Now i have this variable:
$productID = 1;
$userID = 1;
$productAttributeArray[0]['attributeID'] = 1;
$productAttributeArray[0]['value'] = red;
$productAttributeArray[1]['attributeID'] = 2;
$productAttributeArray[1]['value'] = Nina;
Now I search the cartID for my variables. In this sample its the CardID 4. The result must be empty or the cartID
I hope it's the better example.

How to display dynamic mysql vertical data to horizontal using php

I want to display dynamic mysql vertical data to horizontal in html table using PHP. And my table is like
mysql> select * from role_perm;
-------------------------------------------
| id | userID | roleID | permID | value |
--------------- ---------------------------
| 1 | 2 | 1 | 1 | 1 |
|------------------------------------------
| 2 | 2 | 1 | 2 | 0 |
|------------------------------------------
| 3 | 2 | 1 | 3 | 0 |
|------------------------------------------
| 4 | 2 | 2 | 4 | 0 |
-------------------------------------------
| 5 | 2 | 2 | 1 | 1 |
|------------------------------------------
| 6 | 2 | 2 | 2 | 1 |
|------------------------------------------
| 7 | 2 | 2 | 3 | 0 |
|------------------------------------------
| 8 | 2 | 2 | 4 | 1 |
-------------------------------------------
| 9 | 5 | 1 | 1 | 1 |
|------------------------------------------
| 10 | 5 | 1 | 2 | 0 |
|------------------------------------------
| 11 | 5 | 1 | 3 | 0 |
|------------------------------------------
| 12 | 5 | 1 | 4 | 0 |
-------------------------------------------
and so on...
and i want to display in html table like
----------------------------
| role | permissions |
----------------------------
| 1 | 1 | 2 | 3 | 4 |
----------------------------
| 2 | 1 | 2 | 3 | 4 |
----------------------------
| 3 | 1 | 2 | 3 | 4 |
-----------------------------
| 4 | 1 | 2 | 3 | 4 |
----------------------------
could you pls help me. Thank you in advance.
Try this:
SELECT
roleID AS role,
GROUP_CONCAT(DISTINCT permID ORDER BY permID ASC SEPARATOR '|') AS permissions
FROM role_perm
GROUP BY roleID
ORDER BY roleID
I suppose you could do this with a nifty MySQL query as well (which would supposedly be better for performance), but since I'm not that good at SQL, here's the PHP solution:
$arrRoles = array();
while ($row = mysql_fetch_assoc($result)) {
if (!isset($arrRoles[$row['roleID']])) {
$arrRoles[$row['roleID']] = array();
}
$arrRoles[$row['roleID']][$row['permID']] = $row['value'];
}
var_dump($arrRoles);

PHP and MYSQL select with 5 tables and multiple rows as a single array

Would like to get the following as a result from the table structure below (MYSQL + PHP)
array[0][name]1,[desc]red,[title]hero,[desc]strong,[desc2]smells,[img][0]red1,[img][1]red2,[img][2]red3,ext[0].jpg,[ext][1].gif,[ext][2].png,[count][0]253,[count][1]211,[count][2]21,[count][3]121,[dist][0]5,[dist][1]5,[dist][2]12,[dist][3]2,[score][0]2,[score][1]3,[score][2]1,[score][3]5,[score][4]4,[val][0]5,[val][1]1,[val][2]4,[val][3]3,[val][4]4
The problem I have with a simple SELECT, JOIN and GROUP_CONCAT is that the values duplicate after selecting all the images.
I've tried various other ways for example selecting the data by row combined with a foreach loop in PHP, but I end up with lots of duplicates, and it looks very messy.
I also though about splitting it into multiple selects instead of using one, but I really would like to know if it can be done with one select.
Could someone help me with an MYSQL select? Thanks
game
+-----+----------+
| pid | name |
+-----+----------+
| 1 | red |
| 2 | green |
| 3 | blue |
+-----+----------+
detail
+-----+------+--------+-------+--------+
| id | pid | title | desc | desc 2 |
+-----+------+--------+-------+--------+
| 1 | 1 | hero |strong | smells |
| 2 | 2 | prince |nice | tall |
| 3 | 3 | dragon |big | green |
+-----+------+--------+-------+--------+
image
+-----+-----+-----+----+
| id | pid | img |ext |
+-----+-----+-----+----+
| 1 | 1 | red1|.jpg|
| 2 | 1 | red2|.gif|
| 3 | 1 | red3|.png|
+-----+-----+-----+----+
devmap
+-----+-----+-------+------+
| id | pid | count | dist |
+-----+-----+-------+------+
| 1 | 1 | 253 | 5 |
| 2 | 1 | 211 | 5 |
| 3 | 1 | 21 | 12 |
| 4 | 1 | 121 | 2 |
+-----+-----+-------+------+
stats
+-----+-----+-------+------+
| id | pid | scrore| val |
+-----+-----+-------+------+
| 1 | 1 | 2 | 5 |
| 2 | 1 | 3 | 1 |
| 3 | 1 | 1 | 4 |
| 4 | 1 | 5 | 3 |
| 5 | 1 | 4 | 3 |
+-----+-----+-------+------+
When you do a JOIN that involves more than a 1:1 mapping between tables you're going to have duplicate data, and there's no way to get around that in the query.
You can break it out into multiple selects, or you can loop through the result set and pare out whatever duplicate information you don't want.

Replacing the zero with next same id's value - MySQL

I used another table and generated a MySQL views, by using that view I coded the below:
SELECT staffid,
CASE Frequency
WHEN 'Less' THEN COUNT(Frequency)
ELSE '0'
END AS LessCount,
CASE Frequency
WHEN 'Full' THEN COUNT(Frequency)
ELSE '0'
END AS FullCount,
CASE Frequency
WHEN 'More' THEN COUNT(Frequency)
ELSE '0'
END AS MoreCount
FROM effort_frequency
GROUP BY staffid, Frequency
I used the above code to get the below table:
+-------+-----------+-----------+-----------+
| staff | lessCount | FullCount | MoreCount |
+-------+-----------+-----------+-----------+
| 10 | 2 | 0 | 0 |
| 10 | 0 | 1 | 0 |
| 10 | 0 | 0 | 3 |
| 11 | 1 | 0 | 0 |
| 11 | 0 | 3 | 0 |
+-------+-----------+-----------+-----------+
I need this to be converted, which should look like:
+-------+-----------+-----------+-----------+
| staff | lessCount | FullCount | MoreCount |
+-------+-----------+-----------+-----------+
| 10 | 2 | 1 | 3 |
| 11 | 1 | 3 | 0 |
+-------+-----------+-----------+-----------+
How we can do this in MySQL ?
SELECT staffid AS staff,
SUM(Frequency='Less') AS lessCount,
SUM(Frequency='Full') AS FullCount,
SUM(Frequency='More') AS MoreCount
FROM effort_frequency
GROUP BY staffid

Categories