The right way to scale back merchandise amount in inventory? – PHP – SitePoint Boards


For instance, there are 20 merchandise within the inventory desk,when scale back 5 quantity inform there may be 15 in desk inventory

stoch desk

promote desk
2022-08-29_191253


You’d use a UNION ALL question to calculate the entire when wanted, getting the SUM() from the inventory desk, grouped by the merchandise id, minus the SUM() from the promote desk, grouped by the merchandise id. Because you didn’t submit full column data, and assuming you need the entire for merchandise id = 62 (or any set of ids), the question would appear like this –

SELECT i.title, SUM(x.qty) qty
 FROM gadgets i
 LEFT JOIN (
 SELECT item_id, SUM(qty) qty FROM inventory WHERE item_id IN(62) GROUP BY item_id
 UNION ALL
 SELECT item_id, -SUM(qty) qty FROM promote WHERE item_id IN(62) GROUP BY item_id
) x ON i.id = x.item_id
GROUP BY i.id
ORDER BY i.title

This assumes that your information is correctly normalized and you’ve got an merchandise desk, the place the gadgets are outlined. This produces an merchandise id that you’d us in any inventory or promote data to narrate the merchandise again to its definition.

If the Implant_s type subject is the id of the chosen Implant System, wouldn’t you employ that worth to find out which row within the part desk to function on?

Right here’s a laundry record of points and issues that may be simplified within the posted code –

  1. A header() assertion doesn’t cease php code execution. You want an exit/die assertion after the ‘admin’ redirect to cease all the remainder of the code from being executed.
  2. The submit technique from processing code needs to be above the beginning of the html doc, with the intention to resolve what to do on the web page based mostly on the results of the shape processing code.
  3. Don’t use the @ error suppressor, ever. After the shape has been submitted, apart from unchecked checkbox/radio fields, all type fields might be set and can exist. In the event you have been getting undefined index errors from this code, it was as a result of one thing you wanted to search out and repair, not disguise the errors for.
  4. Don’t attempt to take a look at if the submit button is ready. There are instances the place it received’t be. It’s best to as an alternative detect if a submit technique type was submitted. In the event you can have multiple type processing code on a web page, use a hidden subject with a novel worth in it to manage which type processing code to execute.
  5. Regardless of the output() perform code is doing might be incorrect for enter information. Aside from trimming information, with the intention to detect if all white house characters have been entered, it is best to NOT modify enter information earlier than utilizing it.
  6. It’s best to maintain the shape information as a set in a php array variable, then function on components on this array variable all through the remainder of the code.
  7. When you do merchandise #6 on this record, you may trim all of the enter information without delay utilizing one single php line of code.
  8. After trimming the info, it’s good to validate every enter individually, storing person/validation errors in an array utilizing the sector title because the array index. For instance, if Implant_s and Impalnt_n are ‘required’, it’s good to validate that they include permitted values. In the event that they don’t, that’s an error and you need to setup messages for the person telling them what’s flawed with the info that they submitted.
  9. After the tip of all of the validation logic, if the array holding the person/validation errors is empty, use the submitted type information.
  10. You probably have greater than about 2-3 type fields it is best to use a data-driven design, the place you could have a knowledge construction (array) that holds a definition of the anticipated type fields, what validation to carry out on every one, and what processing to carry out. You’d then loop over this definition and person general-purpose code to validate and course of the info.
  11. Typically, conditional logic ‘failure’ code is far shorter than the ‘success’ code. In the event you invert the situation being examined and put the ‘failure’ code first, it’s simpler to observe what your code is doing.
  12. It’s best to construct sql queries in php variables. This makes debugging simpler. You’ll be able to echo the sql question assertion to see what it’s. This additionally separates the sql question syntax from the php code, lowering the variety of typo errors.
  13. It’s best to record out the columns you’re deciding on in a question. This helps forestall errors and makes your question self-documenting.
  14. Don’t copy variables to different variables for nothing.
  15. In the event you set the default fetch mode to assoc whenever you make the database connection, you don’t must specify it in every fetch assertion.
  16. In the event you use implicit binding, by supplying an array of knowledge to the ->execute([…]) name, you may enormously simplify all of your code.
  17. In the event you use positional ? place-holders, you may enormously simplify all of your code.
  18. Go away columns out of an insert question in case you are not supplying values for, e.g. the id column.
  19. If this INSERT question can lead to duplicate information, it’s good to have error dealing with for the question to detect if a replica error occurred, and setup a message for the person letting them know what was flawed with the info that they submitted.
  20. After the tip of all the shape processing logic, if there aren’t any errors, redirect to the very same url of the present web page to trigger a get request for the web page. It will forestall the browser from attempting to resubmit the shape information.
  21. If you wish to show a one-time success message, retailer it in a session variable, then take a look at, show, and clear the session variable on the acceptable location within the html doc.
  22. An empty motion=“” attribute is definitely invalid html5. To get the shape to undergo the identical web page, merely miss all the motion attribute.
  23. In the event you put the <label>...</label> tags across the subject they belong with, you may miss the for=‘…’ attribute and the matching id=‘…’ attribute (which most are lacking/don’t match anyhow.) Solely use attributes in your markup when they’re wanted.
  24. The primary <possibility> in an inventory needs to be a immediate to pick one of many decisions. The worth for this primary possibility needs to be an empty string. It will require the person to truly make a selection, it’s going to allow you to validate the enter (an empty string received’t go validation), and it’ll let the ‘required’ attribute work.
  25. It seems that the code to set the ‘chosen’ attribute for the ‘physician’ subject is leftover from one thing else and must be debugged.
  26. You want code for the ‘Implant_s’ subject to set the ‘chosen’ attribute.
  27. In the event you use a sort=‘date’ subject for dates, all fashionable browsers will current a date-picker.
  28. The type_of_c (sort of crown) choose/possibility must have its values outlined in a database desk, use the id as the sector worth and because the inserted worth, and set the ‘chosen’ attribute, much like the opposite choose/possibility fields.

Lastly, I like to recommend that you just get your code to completely work with ONE type subject of every normal sort, then you may fear about all of the code wanted for the remainder of the fields.



2 Likes

Why does your type for a affected person congratulate the person on including a pupil in kurdish?

Right here’s a number of extra factors so as to add to the record –

  1. Solely retailer the person’s id in a session variable to point who the logged in person is.
  2. Question on every web page request to get the present person’s permissions or another person data.
  3. Apply htmlentities to any dynamic worth whenever you output it in a html context to assist forestall cross website scripting.

I need the thought in two fields in two completely different tables,
I wish to scale back the variety of Impalnt_n culomun of the varieties instantly scale back of the column Implant_s of othere desk

Programming includes defining what inputs you could have, what processing you’ll do based mostly on these inputs, and what consequence you are attempting to provide or output.

For any submitted Implant_s and Implant_n values, what are you going to do if the Implant_n worth is bigger than the part column within the row comparable to the Implant_s id worth? What are you going to do if Implant_n worth is lower than or equal to the part column within the row comparable to the Implant_s id worth?

Reply these questions in your native language, put the 2 solutions into your code as feedback, then try to design, write, take a look at, and debug the code and question(ies) wanted to perform these two statements.



1 Like

i put complite supply code

You should carry out this replace as one single (atomic) operation or you need to lock the desk to forestall a number of situations of the code altering values between the SELECT question and the UPDATE question (which was proven within the code within the now deleted reply #3.)

To do that all inside one single UPDATE question and verify if the worth was modified, see the next –

$sql = "UPDATE courses SET part = IF(part>=?,section-?,part) WHERE id=?";
$stmt = $db->put together($sql);
$stmt->execute([ $Impalnt_n, $Impalnt_n, $Implant_s ]);

if($stmt->rowCount())
{
	// the part worth was up to date, i.e. part was >= $Impalnt_n
	echo 'replace was profitable';
}
else
{
	// the part worth was unchanged, i.e. part was < $Impalnt_n
	echo 'there was not adequate amount to replace';
}



1 Like

Are you able to broaden on “didn’t work” please?



1 Like

What I meant was, what occurred that ought to not have occurred, or what didn’t occur that ought to have? In what means did the code “not work”?



2 Likes

No outcomes appeared within the desk

when information added to desk confirmed this > echo ‘there was not adequate amount to replace’;

<possibility <?php echo $row['id'] ?>><?php echo $row['level']; ?></possibility>
                              <?php } ?>

This received’t generate html which can present a price to the submit object. It needs to be

<possibility worth="<?php echo $row['id'] ?>"><?php echo $row['level']; ?></possibility>
                              <?php } ?>



1 Like

identical shing,it does’t working

One thing’s lacking

Have you ever echo’d out the values of $Impaltn_n and $Implant_s to make sure the values are what you’re anticipating?

Have you ever taken your SQL assertion with these values and executed it to make sure it’s working and there’s not an issue like a subject title being named incorrectly?

And this seems to be flawed…doesn’t rowcount() return various rows affected?

if($stmt->rowCount())

So shouldn’t that be

if($stmt->rowCount() > 0) // possibly -1, been some time with PHP



1 Like

Within the now deleted image (reply #3) of your database desk values, you already had a -300 for one of many part row values. Is that this the one you’re deciding on to make use of? In that case, there may be NOT a adequate amount in that row.



1 Like

Is your id column completely satisfied to have NULL inserted into it? If it’s an auto-increment distinctive ID, simply go away it out of the question altogether.



1 Like

workking now, thanks for serving to my bro and different buddies

Now, when you would simply do the issues on the given lists, in order that your code might be safe in all contexts, present a very good Consumer eXperience (UX), might be easy with out numerous pointless typing, and can both work or it’s going to inform you why it doesn’t.



1 Like

It is likely to be useful to others sooner or later when you might submit the answer – how did you repair the issue?



Supply hyperlink

Leave a Reply

Your email address will not be published.