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.
Related
On my website, I have a users table and industry table and industry_overview table. industry_overview contain the overview data of an industry.
I want to have a "Add to Cart" function where user can add the overview detail such as value chain, market share and market price into their cart list.(Cart list item will eventually be merged and generate a report out in pdf form)
May I know what is the simplest way I could get something like that done? I've thought about this but I can't seem to find a solution or idea.
I don't need a ready-made script, just any idea that could get me working then will be great! (Also if you already have an example of such function, I'd be more than happy to have a look at it).
Thanks in advance!
Update:
Industry overview table has 5 column:
overview_id
industry_id
value_chain
market_share
market_price
I would do it using carts and cart_items tables:
carts
=====
- id
- user_id
cart_items
==========
- id
- cart_id
- industry_overview_id
- column
If you want a user to have multiple simultaneous carts you can add a name column, which the user can change.
Any data point (column in industry_overview) selected by the user will result in an entry in the cart_items table.
However, with this method a user's cart items will automatically be "updated" if the value of the overview item changes. If you don't want this you could save the exact values from industry_overview into the cart_items table, either instead of or in addition to the id.
I need to insert a newly added product in table. But to do so I need to check SKU of last inserted product and increase it by 1 and insert it with new product details. SKU is something like SKUNXXXXX - X is a digit. Changing table structure is not possible.
Possible solutions that I can think of is
Get last row using order by and limit 1.
replace "SKUN" with empty string and increase the number by 1
Insert record with product details and incremented SKU value
But this situation may create a problem(though I am not sure about it). Problem is - what if just after fetching last record and before inserting the new product details, another request comes in and gets the same last record? In this case both of the products have same SKU.
Please let me know how to solve this situation.
One option would be to create an auto increment id column and then use this to create the SKU for each product as it gets inserted. A trigger, which would fire after an INSERT, could then be used to assign the SKU for the newly inserted product. But alas, MySQL does not allow a trigger to modify the table which fired it.
However, there is a potentially easy workaround here. If your SKU values will really always have the format SKUNXXXXX, where XXXXX is some number, then you can simply add an auto increment column, and create the SKU value on the fly when you query for it. So you might create a table called products with the following columns:
CREATE TABLE products
(
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(55),
...
);
Everytime you do an INSERT on products, the id column will automatically be incremented by MySQL. When you want to get the SKU out, you can simply do a query like this:
SELECT id, name, CONCAT('SKUN', id) AS `SKU`
FROM products
WHERE name = 'someproduct'
If you want the SKU number to not be forever (i.e. once assigned to a given product, it can be reassigned to another new one), then you might consider resetting the auto increment id column, q.v. this SO post for more information.
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.
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.
I have a software in PHP and postgres that I use for invoicing. I am running a stock management system that I created. i am trying to create a stock movement page where I can see when a part came in, where it was issued and when and also when it was credited (if it was). I am running 5 tables for the stock. My main one is part2vendor, parts, expenses, wo_parts and int_part_issue. When I receive stock, it goes into the table part2vendor (onhand gets increased by number received). The expense table gets the details of the part number, the supplier invoice and the date received. wo_parts stores the part number issued to a workorder. int_part_issue is when I do a stock adjustment or use a part internally. I am looking to create a PHP table that would list (in date order) the 'paper trail' of a part. I can let you know table names and columns if required. Thanks.
Sounds like you need a simple history table?
Columns
part_history
id
part_id
date_modified (timestamp)
action ( or maybe action_id if you have an actions table)
vendor_id
And when you get a new part, and add it to the parts2vendor table ( i think u said) you would use the inserted part ID (or unique part id ) to add a record rto history
INSERT
(id, part_id, action, vendor_id)
46565, 5757575, "Purchased", 757575
The date will be inserted as a timestamp by postgres
Than for any part yuou can grab history relying on the uniquer id
sort by date_modified DESC.