At the risk of taking the fun out of doing things yourself - at the point that the visitor has committed to purchasing the items in the cart, you would first insert a record into an 'orders' database table, with the unique/one-time order information - id (auto-increment primary index), user_id (id of who placed the order), datetime (when was the order placed), and ship_to_id (id of where the order will be shipped to), and any other useful information about the order. You would get the last insert id from the above query and use it as the order_id when inserting rows into an 'order_items' table, with a row for each item in the order - id (auto-increment primary index), order_id, item_id, quantity, and any other useful information about each item in the order.
If you are asking how to implement this code/query(ies), you need to learn the fundamentals involved with securely building and executing an INSERT query using php. You would want to use the php PDO extension, use a prepared query, use TRUE (non-emulated) prepared queries, use implicit binding (supply an array of input data to the ->execute([...]) call), and use exceptions for database statement errors.