ok first off I know how to make a update page with usernames and user info no problem.. I know how to do stock with the stock=stock -%d as a interger but what I am having a hard time with is I have no clue how to say for example to update a pick list...

My exmaple - my pick list \/

producta - 5

-----------------------------

Now I want to update my pick list to ----\/

proudcta - 4
productb - 3

Then once I hit save it automatically adds 1 back into the total stock avaibale for producta and takes out 3 out of inventory from productb..

Any thoughts on how I would do this... Any pointers or suggestions or ideas to point me in a firm direction would be greatly appriecated.

Thanks

Dani AI

Generated

Short answer: treat the edit as a delta between the saved picklist and the new one, then apply that delta to inventory inside a transaction. your intuition (compare old vs new and adjust stock up or down) is correct — the missing pieces are reliably getting the original quantities from the database, computing per-product deltas, and doing the updates atomically so concurrent edits don't corrupt stock.

How to do it (high level)

  1. Load the saved picklist from your DB (order_items table: product_id, qty). Do not trust hidden form values for the original.
  2. Parse and validate the submitted picklist (integers only, >=0).
  3. For each product compute delta = old_qty - new_qty. If delta > 0 you add stock back; if delta < 0 you remove stock.
  4. Start a DB transaction, SELECT ... FOR UPDATE the involved product rows, ensure decreases won’t make stock negative, run the updates, save the new picklist rows, then COMMIT. Roll back on any failure.

Example: build deltas in PHP

// $oldItems and $newItems are arrays: product_id => qty
$all = array_unique(array_merge(array_keys($oldItems), array_keys($newItems)));
$deltas = []; // positive => return to stock, negative => take from stock
foreach ($all as $pid) {
    $old = isset($oldItems[$pid]) ? (int)$oldItems[$pid] : 0;
    $new = isset($newItems[$pid]) ? (int)$newItems[$pid] : 0;
    $delta = $old - $new;
    if ($delta !== 0) $deltas[$pid] = $delta;
}

Apply deltas safely with PDO + transaction

$pdo->beginTransaction();
// lock rows
$ids = array_keys($deltas);
$in = implode(',', array_fill(0, count($ids), '?'));
$stmt = $pdo->prepare("SELECT id, stock FROM products WHERE id IN ($in) FOR UPDATE");
$stmt->execute($ids);
$stocks = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
// validate and update
foreach ($deltas as $pid => $delta) {
    $avail = isset($stocks[$pid]) ? (int)$stocks[$pid] : 0;
    if ($avail + $delta < 0) throw new Exception("Not enough stock for $pid");
}
$upd = $pdo->prepare("UPDATE products SET stock = stock + ? WHERE id = ?");
foreach ($deltas as $pid => $delta) $upd->execute([$delta, $pid]);
$pdo->commit();

Notes and troubleshooting: use InnoDB, prepared statements, validate all inputs, log changes for audits, and test with concurrent edits. This approach covers the example you gave (productA 5->4 returns 1 to stock; productB 0->3 removes 3) and scales cleanly.

Recommended Answers

All 11 Replies

Member Avatar for Member #949455

Any thoughts on how I would do this... Any pointers or suggestions or ideas to point me in a firm direction would be greatly appriecated.

It would be nice if you actually have the query and php code. It would be easier if you can post what you have so we can have a better understanding.

commented: To Rectify what some retard did to LastMitch +0
commented: Cancel out undeserved neg rep. +13

:) ha yes it would be wonderful.. but If I have no idea how to do something and ask for help how to do something its hard to have something already built... Thats why I came here and asked for help. Sorry for my english..

Member Avatar for Member #949455

:) ha yes it would be wonderful.. but If I have no idea how to do something and ask for help how to do something its hard to have something already built... Thats why I came here and asked for help. Sorry for my english..

I think you been here long enough to know what no one will write the whole script for you. If you have at least something to start with then we can help you but again it's gonna be hard for anyone to write the code for you.

commented: To Rectify what some retard did to LastMitch +0
commented: Cancel out undeserved neg rep. +0

I think you been here long enough to know what no one will write the whole script for you. If you have at least something to start with then we can help you but again it's gonna be hard for anyone to write the code for you.

Hey lastmitch why do you do this? Is it a power trip thing? People like you are why people like me hate to come on forums because I ask one question or a idea on how to do something and then you get all huffy and puffy and say stuff like you did and waste time.. I come here for the code, I come here to learn, I come here to try improve on things.. Are you having a bad day or something? You can pm and tell me about it.

Anyways Again I aplogize for my english as I did before. I have no idea how you got the idea of GIVE ME THE SOURCE CODE out of that. Now what I was trying to ask again is about - the how would you do something like that.. Or what would be a good way.. My best theory was the integer in php like the example I posted above..

Example - use - stock=stock -%d and integer in form box..

but then the calulation.. Maybe If statement If item number in edit form is below --saved-- number then stock=stock +%d2 - adding one back into the stock..

and If statement is above ----- stock=stock -%d1

Do you get what I am getting at? Does this make any sense what I am trying say? Does this theory seem stupid? Good bad? I'm sure any suggestions about code or how to do this would greatly help me

thanks

commented: This is rude and disrespectful! -1
Member Avatar for Member #949455

Hey lastmitch why do you do this? Is it a power trip thing? People like you are why people like me hate to come on forums because I ask one question or a idea on how to do something and then you get all huffy and puffy and say stuff like you did and waste time.. I come here for the code, I come here to learn, I come here to try improve on things.. Are you having a bad day or something? You can pm and tell me about it.

Well, There's no need to be this rude. Second of all if you came here to learn then most likely I or someone else would help you. Regarding about a bad day, now I didn't have a bad day nor will I PM anyone on Daniweb about my feeling.

Anyways Again I aplogize for my english as I did before. I have no idea how you got the idea of GIVE ME THE SOURCE CODE out of that. Now what I was trying to ask again is about - the how would you do something like that.. Or what would be a good way.. My best theory was the integer in php like the example I posted above..

Since you down voted me most likely I won't even assist you to write this code. You're not good in English is not a excuse to write something like this rude and expect me to pretend that this did not happend and expect me to help you?

:) ha yes it would be wonderful.. but If I have no idea how to do something and ask for help how to do something its hard to have something already built... Thats why I came here and asked for help. Sorry for my english..

This is a hint that I make a conclusion that you need help to write the whole code. you mention you have no idea how to write it and you said it's hard to have something already built. It means that you have an idea but no way of put it into a code. Does that make sense.

commented: To Rectify what some retard did to LastMitch +0
commented: Cancel out undeserved neg rep. +0

LastMitch Really? What sort retard are you? I asked for help with code not be bothered by a pain in the ass like you.

commented: You're retarded. -3
Member Avatar for Member #949455

LastMitch Really? What sort retard are you? I asked for help with code not be bothered by a pain in the ### like you.

That is really uncalled for. Why did you downvoted me 200 times?

commented: To Rectify what some retard did to LastMitch +0
commented: Cancel out undeserved neg rep. +0

help with code

Really? Did you? I think you're implying that someone does the work for you, rather than hand someone a piece of code.. Please, don't take offence to this but you can't hardly call anyone here (Who, may I add is taking time out of their lives to HELP you) a "Retard" - Learn some respect before you even contemplate PHP/MYSQL.

What is wrong with you? You expect someone to help you... you don't deserve help if you give back with bull****... You should really be gone from any forum!

You got someone like LastMitch to actually look at your thread and reply to it, you were lucky because he, by average, gets you the answer in the first try but sadly you'll never get your questions answered.

I feel bad for anyone even noticing this thread and espicially for LastMitch.

Hello anyone? anyone? buller? buller? buller?

haha Are you kidding me lastmitch is the boner that started all of it. I just asked on thoughs my english is horrible but not that horrible where people can undertsand the thought of asking how to do something.. To all the d-bags out there I never asked for anyone ever to code this out for me.

commented: Troll. +0

You're awesome xbat.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.