For example, there are 20 products in the stock table,when reduce 5 number tell there is 15 in table stock
stoch table
sell table
For example, there are 20 products in the stock table,when reduce 5 number tell there is 15 in table stock
stoch table
sell table
You would use a UNION ALL query to calculate the total when needed, getting the SUM() from the stock table, grouped by the item id, minus the SUM() from the sell table, grouped by the item id. Since you didnât post complete column information, and assuming you want the total for item id = 62 (or any set of ids), the query would look like this -
SELECT i.name, SUM(x.qty) qty
FROM items i
LEFT JOIN (
SELECT item_id, SUM(qty) qty FROM stock WHERE item_id IN(62) GROUP BY item_id
UNION ALL
SELECT item_id, -SUM(qty) qty FROM sell WHERE item_id IN(62) GROUP BY item_id
) x ON i.id = x.item_id
GROUP BY i.id
ORDER BY i.name
This assumes that your data is properly normalized and you have an item table, where the items are defined. This produces an item id that you would us in any stock or sell records to relate the item back to its definition.
If the Implant_s form field is the id of the selected Implant System, wouldnât you use that value to determine which row in the section table to operate on?
Hereâs a laundry list of issues and things that can be simplified in the posted code -
<label>...</label>
tags around the field they belong with, you can leave out the for=ââŠâ attribute and the matching id=ââŠâ attribute (which most are missing/donât match anyways.) Only use attributes in your markup when they are needed.<option>
in a list should be a prompt to select one of the choices. The value for this first option should be an empty string. This will require the user to actually make a choice, it will let you validate the input (an empty string wonât pass validation), and it will let the ârequiredâ attribute work.Lastly, I recommend that you get your code to fully work with ONE form field of each general type, then you can worry about all the code needed for the rest of the fields.
Why does your form for a patient congratulate the user on adding a student in kurdish?
Hereâs a few more points to add to the list -
I want the idea in two fields in two different tables,
I want to reduce the number of Impalnt_n culomun of the forms directly reduce of the column Implant_s of othere table
Programming involves defining what inputs you have, what processing you are going to do based on those inputs, and what result you are trying to produce or output.
For any submitted Implant_s and Implant_n values, what are you going to do if the Implant_n value is greater than the section column in the row corresponding to the Implant_s id value? What are you going to do if Implant_n value is less than or equal to the section column in the row corresponding to the Implant_s id value?
Answer these questions in your native language, put the two answers into your code as comments, then attempt to design, write, test, and debug the code and query(ies) needed to accomplish those two statements.
i put complite source code
You must perform this update as one single (atomic) operation or you must lock the table to prevent multiple instances of the code changing values between the SELECT query and the UPDATE query (which was shown in the code in the now deleted reply #3.)
To do this all within one single UPDATE query and check if the value was changed, see the following -
$sql = "UPDATE classes SET section = IF(section>=?,section-?,section) WHERE id=?";
$stmt = $db->prepare($sql);
$stmt->execute([ $Impalnt_n, $Impalnt_n, $Implant_s ]);
if($stmt->rowCount())
{
// the section value was updated, i.e. section was >= $Impalnt_n
echo 'update was successful';
}
else
{
// the section value was unchanged, i.e. section was < $Impalnt_n
echo 'there was not sufficient quantity to update';
}
Can you expand on âdidnât workâ please?
What I meant was, what happened that should not have happened, or what did not happen that should have? In what way did the code ânot workâ?
No results appeared in the table
when data added to table showed this > echo âthere was not sufficient quantity to updateâ;
<option <?php echo $row['id'] ?>><?php echo $row['level']; ?></option>
<?php } ?>
This wonât generate html which will provide a value to the post object. It should be
<option value="<?php echo $row['id'] ?>"><?php echo $row['level']; ?></option>
<?php } ?>
same shing,it doesât working
Somethingâs missing
Have you echoâd out the values of $Impaltn_n and $Implant_s to ensure the values are what youâre expecting?
Have you taken your SQL statement with those values and executed it to ensure itâs working and thereâs not a problem like a field name being named incorrectly?
And this looks wrongâŠdoesnât rowcount() return a number of rows affected?
if($stmt->rowCount())
So shouldnât that be
if($stmt->rowCount() > 0) // maybe -1, been a while with PHP
In the now deleted picture (reply #3) of your database table values, you already had a -300 for one of the section row values. Is this the one you are selecting to use? If so, there is NOT a sufficient quantity in that row.
Is your id
column happy to have NULL inserted into it? If itâs an auto-increment unique ID, just leave it out of the query altogether.
workking now, thanks for helping my bro and other friends
Now, if you would just do the things on the given lists, so that your code will be secure in all contexts, provide a good User eXperience (UX), will be simple without a lot of unnecessary typing, and will either work or it will tell you why it doesnât.
It might be helpful to others in the future if you could post the solution - how did you fix the problem?