Frankie Gee
Frankie Gee

Reputation: 1

updatepricing in MySQL where multiple prices are in same row

I have been tasked to update the price list on our website. Currently, we have to do this one item at a time inside the Admin Panel.

However, we have over 3000 items, and tiered pricing for each item (up to 5 tiers)

Problem is, in the sell_prices table, the prices are structured like so:

10.50:9.50:8.50;7.50;6.50 in one cell.

I am attempting to write a script that will update each sell_price by 10%

UPDATE inv_items
SET sell_prices = sell_prices * 1.10
WHERE id = xxxxxx

I have also tried:

UPDATE inv_items
SET sell_prices = sell_prices * 1.10; sell_prices = sell_prices * 1.10
WHERE id = xxxxxx

But naturally received an error.

This works great but only updates one record, and leaves the rest blank.

Currently, I am working through PhpMyAdmin but I will write a new Price Increase script once I can figure this out.

I have backed up the database.

Upvotes: 0

Views: 98

Answers (3)

Скач от
Скач от

Reputation: 212

EDIT with mysqli function as suggested:

$qry = 'SELECT id, sell_prices FROM `tablename`';
if($result = $mysqli->query($qry)) {
    while ($row = $result->fetch_assoc()) {
        $temp = explode(';', $row['sell_prices']);
        foreach ($temp as &$price) {
            $price = (float)$price*1.1;
        }
        $prices[$row['id']] = implode(';', $temp);
    }
    foreach ($prices as $id => $pricesStr) {
        $stmt = $mysqli->prepare("UPDATE `tablename` SET sell_prices = ? WHERE id = ?");
        $stmt->bind_param('si', $pricesStr, $id);
        $stmt->execute(); 
        $stmt->close();
    }
}

Please note that I wrote this on the fly without testing, i may overlooked something :)

Upvotes: 0

gedq
gedq

Reputation: 610

If I understand you correctly then you have 5? prices in one field, colon separated?

That is a really bizarre way of doing it. There may be a nifty way of doing it with mySQL parsing, but from PHP you're going to need to pull the values out, explode them into an array, apply the price increase to each element, implode it back with the colons and write it back to the database. It's as clunky as all get-out but faster than doing it by hand. Just.

Going forward if you can you really need to look at refactoring that; that's just going to keep biting you.

Upvotes: 1

ethrbunny
ethrbunny

Reputation: 10469

You'll need to do something like:

select sell_prices from inv_items
(get the values into php)
(split the values by delimiter ':')
(update each value)
(rebuild the line of values with ':' in between)
update inv_items set sell_prices = (value string you just created)

Upvotes: 0

Related Questions