How do you control the database update using an EDIT function?
How do you control the database update using an EDIT function?
I have the editor setup for a products table. I want to have a field that when a person edits a product, they can add stock. Is there a way to have a field called "Add Stock" and then control the table update so that it adds the new number to the "currentstock" table column?
This question has accepted answers - jump to:
This discussion has been closed.
Answers
Just to add clarification.
I have a table displayed and when the user selects a row and clicks on the EDIT button, when the popup comes up it has all form fields for the products table.
One of the fields is "Current Stock" which shows the numeric value of current stock on hand which is readonly for display.
I want to have a form field called "Add Stock".
When the user adds a value in "Add Stock", when the update button is clicked I need to take the form field value submitted and ADD it to the currentstock value on update.
Example:
Product ID: XYZ
Current Stock: 150
Add Stock: 50
On update, the currentstock field for the product id will now be 200.
Update:
I decided to add a custom button called "Add Stock". It opens an edit window that shows the current stock as "readonly" and another form field called "Add Stock".
My question is, when I add stock, on save, I want it to ADD the new amount to the readonly field to the database AND make an entry in another table called productstock which makes an entry for the new amount for transaction logging.
Any suggestions on how to do this?
One option is to use the
preSubmit
option. Using that you can modify the data sent to the server, so you could, for example take the current stock value and just add the "add" value:The other option would be to do this on the server-side using the
setValue
option for thestock
field. Again you would get the current value (possibly reading it form the db if you wanted to ensure there was no possibility of loosing information) add the 'add' value and then return the combination.Regards,
Allan
Thanks Allan
Two questions. Where does the "editor.on" code you put go?
Also, how would I handle "inserting" a row into the productstock transaction log table with the add stock value?
Here is an example of an editor page using a "on" function which shows where code goes ...
http://editor.datatables.net/examples/api/confirmClose.html
Thanks...I got it.
My only problem right now is when I add the stock with:
data.data.stock += data.data.addStock;
If the total was 500 and I add 50, it's updating the current stock to 50050 instead of 550.
I've been trying to wrap each value with parseInt() to no avail.
First question figured out:
Now I just need the ability to perform an insert on another table with the productid to record the transaction in the productstock table.
Two options:
Allan
So I would try a left join during the presubmit?
Just learning all this so sorry for the questions.
What would the left join look like?
So, I have the code below that performs the adding of stock and it works perfectly.
I now need to take the following variables and pass them as an insert to another table:
In the editor, I have productid, description and addstock (hidden) that I can pass to the presubmit, but I also need to pass the current PHP $_SESSION['userid'], $_SESSION['customerid'] and the current date to the productstock table.
Sorry, I'm stumped as to how to do this insert.
No - the Editor at the server-side would be set up for the leftJoin, as per the documentation. You may already have it doing a left join from the data structure?
What does your server side code look like? Are you using the PHP or .NET Editor libraries?
Allan
I'm using PHP libraries. I'm doing a left join on two tables to populate two select boxes in the editor.
Okay thanks - so the join table should already be updated. Reviewing your earlier comments I missed this before:
So you want a new row every time
addstock
is provided? If so, my apologies for misunderstanding. This is not something that Editor itself will do since that is a seperate transaction, however, it is something you can easily do using a splash of PHP:Documentation for the
Database->insert()
method is available here.Allan
Thanks Allan,
Yes. Only when addStock is done, I want an insert into the productstock table that for reporting purposes records each time a user adds stock to the products table.
Where would I put your code:
Got it. Added it in my PHP just after the include DataTables.php
One last thing, here is my code in the PHP page:
I have a column that the leftJoin uses to match the customerid so that it displays the customername in a select list.
However, I also need the customerid to be passed as a variable during the addstock and I can't seem to get it to do that.
I now have it doing an insert with everything EXCEPT the customerid from the row
If I dump out the array coming out of the ADD STOCK it looks like this:
I will post all of my code:
PHP:
JAVASCRIPT:
HTML
I found the issue
In the JS file, it has the following field which with the left join shows the select list with the products customername based on customerid value:
Only, table_products.customerid is now unavaliable. Every other Field::inst() can be passed except the one above as customerid.
Is there a way around this?
For now, inside the
I'm just running a query against the products table to retrieve the customerid in a SELECT query passing the $_POST['data']['table_products']['productid'] and it works fine.
Good to hear that works now :-)
Allan