sql - How I can get an auto incremented value -
i have here table corresponds orders of customers. use auto_increment determine id of order. have sql code orders table:
create table if not exists `orders` ( `order_id` int(11) not null auto_increment, `customer_id` int(11) not null, `customer_name` varchar(500) not null, `order_total_price` decimal(20, 2) not null, `order_date` varchar(100) not null, primary key (`order_id`) ) engine=innodb what need insert each of products of order in table foreign key order_id specify order products belongs to. sql code purchased_products table is:
create table if not exists `purchased_products` ( `order_id` int (11) not null, foreign key (`order_id`) references orders(`order_id`), `product_name` varchar(500) not null, `product_price` decimal(20, 2) not null, `product_quantity` int(11) not null, primary key (`order_id`) ) when user buy something, use insert data in orders table:
insert orders (customer_id, customer_name, order_total_price, order_date) values ('{$customer_id}', '{$customer['customer_name']}', '{$order_total_price}', '{$order_date}')"; and here problem. need insert in purchased_products table products order id generated:
insert purchased_products (order_id, product_name, product_price, product_quantity) values ('*/the id of order need goes here*/', '{$product['product_name']}', '{$product['product_price']}', '{$product['quantity']}')"; this giving me headache. i'm not knowing how it. should done different way? how associate order id products belonging it?
use function last_insert_id(). give value auto-incremented last 1 before call it.
Comments
Post a Comment