A script to update all columns in a mysql database - php

Hi I am using PHP to manipulate information in my MySQL database. However I am looking for a way to update a table (all records need to be updated) based on information within another table.
For example I have a list of products lets say 10 each with a unique id stored in a products table. I have a purchases table which has the same product ID and the amount of purchases done for each product. I want to update each product in the products table to reflect the total purchases made for each product and store it in a column called instock which is part of the products table.
How can this be done?

If I understand your situation correctly, you're dealing with a stock-count. When an item is purchased (represented by a entry in the Products table) then the stock count figure should be decreased. This should happen within the same transaction as the new entry to the Products table to keep your data consistent. I would recommend using a Trigger on the table to implement this. You'll find lots of information about implementing triggers in MySQL on this site. A trigger you could use might look something like this:
CREATE TRIGGER update_stock_count
BEFORE INSERT ON Purchases
FOR EACH ROW
BEGIN
UPDATE Products SET stock_count = stock_count - NEW.quantity_ordered
WHERE product_id = NEW.product_id;
END;
This trigger doesn't take into account that there might not be enough stock of a product, nor does it handle updates or deletes on the Purchases table but it could be modified to do so.

Related

Is creating a new table each time a user logs in a bad practice?

I am developing an online inventory management system. I have one table for the main inventory which consists of the product names, quantity and barcodes etc. I want users to log into the system each morning and log the quantity of each item they take. I then want to store this information for the admin to view. I have developed the system so that a new table gets created each morning based on the users name and date. This is allows the user to input the quantity for each product, i then subtract that column from the main inventory table when the user clicks submit. I want to know if this is a bad practice, is it necessary to create a new table each morning? If not what method should i use?
I think you should maintain a single table for inventory, and then after a create a temporary table to store the list of items the user takes which will contain the user id, inventory id, qty etc, and then update the quantity of inventory in inventory table, whenever user picks the inventory.
In short you should create a single table to store the information of inventory which user takes instead of creating a new table each day.
Insert and fetch data according to date + user.
I wonder if you are creating new table everyday, you should not create new table in any case. In the rarest scenario even if your columns are dynamic then create a table with rows representing as column and use pivot to fetch the record. For your use case you just need to have a table which stores a new record and subtract the count from main table.

Quantity subtraction on multiple db records execute only in case all conditions are fulfilled

Im working on a home project where i have a stock and i need to transfer products from one table to another. The trick is that each product is made of multiple other products from diferent tables:
For example: I define that Product A (will be moved to table u2) is made of:
10pcs of Product B from table u1
5pcs of Product C from table u1
Based on this when i select Product A from a select box and hit a transfer button the defined products need to be subtracted from the database and added or updated as a record in u2 table (Product A). The thing is not all products will have the defined quantity for them in that case i can throw an error, in the other case if the subtraction reaches 0 then i need to remove the respective product from the database.
Because this is a web application i don't know what will happen when multiple users will interact with this. If i make the selects and based on those selects make updates later i don't really know for sure if another user inserted or removed records from the definitions table meanwhile so the updates will fail.
Ideally i would need to make the updates only if all the definitions have enough quantity to subtract from. I looked at TRANSACTIONS but i'm not really sure how to implement them.
Thanks for help!

Some Issue with SQL And PHP

I am currently working on cart system for an assignment.
But, I am facing some difficulties while storing the total item to database.
I have 2 tables, which are Cart and Item table.
Cart table is used to store the Total price of the purchase and payment date.
Item table is used to store the item that the user purchased and the Item table has CartID as a foreign key.
My question is :
while I store the total and payment date to the Cart table, I also need to store the Item that the user purchased to the database. What is the value of the CartID in the Item table? Because, I first store the total and payment date to the database, how to insert the CartID in the table Items?
I have no idea.
The thing you are looking for is "LAST_INSERT_ID()". When you insert the card row you need its ID for the items foreign key.
Your queries should look something like this
INSERT INTO Cart (auto,text)
VALUES(NULL,'text'); # generate ID by inserting NULL
INSERT INTO items(auto,itemTitle,cardID)
VALUES(NULL,'text',LAST_INSERT_ID()); # use ID in second table
If you give us your exact code I can adapt the script to fit your setup but this should give you an guide to what you need.

Inconsistent data when traversing through 4 tables

I have 4 tables (with a lot of fields, but only a few important ones in each) that I'm working with.
Customer table
Inventory table
Invoice table
Invoice details table
What I've been doing is getting a customer or item's id.
If it's a customer, I take their id to get their information from the customer table. Now, in order to get the item's they carry I have to look at each invoice with that customer's id. So I grab all of those. Each invoice has and id that links to an invoice detail table, so I take that invoice id and grab the invoice details that match with the invoice. Then the items are in the iteminventory table, so I take the item id from the invoice detail and get all of the items for every invoice associated with that customer. Then I run that data through a loop to get rid of any empty values and pass the item array to the view.
Now if it's an item the actions are performed vice versa. I take the item, look at each invoice detail containing that wine, grab the invoice id from it, and grab the customer id from each invoice respectively.
Somehow I wind up with customer's carrying a product, but when I go to the product to see what customer's carry it, the previously mentioned customer won't be listed under the product.
Is there an easier way to do this?
Most SQL programmers solve this kind of problem with a single query using a series of JOIN directives and an appropriate ORDER BY directive. They write a bit of php code to read the resultset row by row, detect rows in which the invoice id changes, and format the information appropriately.
It seems like a good idea for you to read up on how to use JOIN directives if you don't already know.

Database Select Query Confusion

I need to keep all purchases from a single client together on a database table (It will be atored in my Orders table) and once someone is browsing a product, the system will search the dB for clients that bought that product and recommend the other products they bought (you will need to check for popularity of the other products and avoid repetition)
The data will be stored in a mysql database in a table called Orders. I then need to be able to search that database to see if other people have bought this product and if so which products they also bought.
So I've come up with this query
SELECT ProductName FROM Orders
(I have little knowledge and would like to no if I am on the right track)
I need to keep all purchases from a single client together on a database table
For this one you probably need to create a horizontal view (restricting by the client id you need to monitor the purchases) CREATE VIEW.
Could you provide your database schema ? in order to create the query you need you should join many tables, so it would be easier to provide the schema and how your tables are tied together.
try this
SELECT ProductName
FROM Orders WHERE client_id in
(SELECT client_id
FROM Orders
WHERE productname="ProductName ");

Categories