Place Order in shopping cart - PHP/MYSQL Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right youtube in this video we are going to see how to place order of a product in this swabi card website this is the seventh video or the seventh series of the shopping cart tutorial so if you see if i am not logged in yet my cart is empty so let me add this product to the cart and if you see this product is added to the cart if you wish to change the quantity you can do it and now i'm going to check out so before checking out i'm not logged in yet so if i click on checkout it me it will throw me to the login.php which is the place where we logged in login so if i uh use the login which i have already used login so it is after clicking on the card checkout it is displaying all the values which i have already filled if you see these are the value which i have already filled and this is a total of the product which i have chose the price is 1500 and i have chose three quantity so it is multiplying and displaying it so if you see let me delete this so that you won't get confused at present the order table is empty so if i click on cash on delivery and accept the terms and condition click on submit your order is placed at present these are static values we'll just look on the database so if you see the order is placed which is cash on delivery order place total price and this is a user id and this is the order the quantity is 3 and the product is 10. fine now if i log out and log create an account let's say p q pqr gmail.com create an account register it is getting me to the checkout page so that i can place some give some values i'm failing it all the values and at present it is nothing so i'm going to get some products let me get this mobile check out it is blank so i have to fill it i'm filling it and the value is over here and this time paypal anything check accept pay now and your order is placed and the database is filled with the order which you are placed right now and if i click log out and login with the same name which you have created right now now checkout.php will have the default address which you have provided while logging in now the next time when you log in uh the next time when you place order you don't have to insert all the fields which is your address the billing address you don't have to mention building address every time while ordering now it's already stored in a database and it will fetch whenever you log in fine let's get into the previous video we have seen how to create account for the customers and after creating account we can able to login into the account and uh check the checkout page so this is account this is account which we have created in the previous video let's let me login it login permit so if you see the checkout page is visible it is because uh registration process and login process these are the two files i have while logging in a session is created so this is a session along with that i want one more [Music] session to be created with the id with the name customer id so it will hold the data let's get into the user data so this is the id so we can get the value from here so it's id now so after login we are redirecting to the checkout.php let me open the checkout.php so here i would like to display both the session variables so that we can check whether the active session is whether this session is whether whether the session is created or not that's what we are going to see echo dollar underscore session with the name email this is one session which we have created concatenate html break and then the so this is the session not the customer because it's the decision customer is the session not the email so if i refresh i should see both the line number 22 undefined customer id so while getting into it it is not created so let us login again now this time we should see the id fine now uh while creating as well while creating account as well i want the session so the same way let me commend this on register process this is a session so likewise let me copy paste it so this time we will get we will be using the insert query so to get the id email and password is a column name if you see here email.password so to get the id we have a function called mysqli insert id just we need to pass the connection so the connection here is this so after getting after account created i want to it will be proceeded to checkout.php so in the checkout.php we should see the session variables like let me log out and login again uh let us create with the id abc at direct gmail.com login so the id is created sorry the session is created with we don't want these two forms we'll just make this one let's make these two forms into one let's head into checkout.php we'll comment this session variable for now we'll make it call md12 let's see where does it ends and we don't need this by refresh column d2l is little bigger let me make its column d6 r8 call md offset 4 so this is the code fine so this is a single form which we have now what we do is i'm going to add the name attribute so before adding name attribute let uh let's add the form tag we'll skip the action and add a method equal to post and we'll place this at the end and we'll make this input type submit value equal to pay now let's see fine now let's add the name attribute as we did for every form we'll give this submit i'm going to give name to all the input field so before that let's check whether we have some other form available if there is some other form we have to remove it because we are surrounding it with a single form yes fine as i said to follow along i'll leave the source code in the description you can get it i'll i'm pausing it i'm giving the name to all the input field so these are the list of names which we are going to use and i have given the name as well fine now let's try to display the text values here instead of echo why don't we use printer and we'll format it using re-tag line up between semicolon so if i submit we should see the list of names country f name last name company name address 1 address 2 city state postal code email phone and then we have and it is not displaying the cod and everything now if you see we could see the payment and agree it is because if i refresh and uh click on check payment and here and pay now and you you should see the check is selected and agree is on fine now on the checkout page so let's write the submit query if dollar underscore post we have given the button input type if it is set then what we do is we'll get the value dollar underscore post and store it into a php variable we have 13 of them let me call it pause it and paste and paste it and finally we'll use the session underscore session customer id which we have created while logging and we'll store it to a variable called dollar user id or customer id customer id so now uh here two things could happen so the first thing is if the data is already inserted then we should just show the date values from the table and display here if he is a new user then this this data should be blank so for that first we'll check whether the data is already there in the database if the data is already there that means the person or the customer already has purchased some of the other product here in our website so if he is new then this page would be blank so for checking we'll use sql select query and we know the id is over here we'll use the same id if he has already purchased then we that he this id would be there in our database if it is new then it should be blank so here select statement and we'll copy this whole thing and paste it over here select all from the table here is user data select all from user data and if the result is equal to 1 that means data is there in that case if the data is there what we have to do is we have to fetch all the data from the table and display it on the form that means we have to use update query if it is null then we have to insert so select start from data where the column name here is user id is equal to the session variable will print some value no not necessary so here we get the insert code we'll write the insert statement over here and we'll give and underscore sql this is the table name and the list of country list of columns which is going to be like i'm going to pause it and paste it over here so i have pasted all the column names now it's time to fill the values the value which we get with these valuable which we will get from the input value input fields and we have stored this into this variable and i just have to copy paste it again i'm going to pause it again and do it fine i have done it now the same way we have to write the update code so update table name here it is going to be the column name and it is going to be the value from the input field and here the id is going to be this id and the relevant column is user id fine let me copy paste it again for this some couple of times so the update query and the insert query is set we have written two queries the first one is insert and the second one is update so now we have to run these two sql queries and insert the values in the database so for that update so it is going to be inserted so this is going to be updated so if it is true then we'll for then what we'll do is what the data is inserted into the database and we have to fill some another table as well which is going to be order table and order two tables and order items so we'll write the query here if updated is working properly to identify both of them i'll use inserted and that is going to be updated so in the terms and condition we have used the check box and we'll use the value equal to true if this is set then only we want to proceed with the sql queries dollar underscore post if is if it is true then only we want to insert it or else echo agree to terms and condition and store this into a variable called message and will display it above form so here i'll define it as null and we are getting into the form where is the form it's over here we'll use the php echo so on page load it is going to be empty string so if i try to submit undefined industry it could not find drp i think i have not used that name properly click on p now on line number 90 control g19 so it is because it could not find the variable so if it does not find the variable will make it over here as false and the payment is also a radio button we'll use that as well it is also this is also showing error that's why i'm doing it so let's try to submit fine now let's work let's type some of the values so the address cities mumbai status maharashtra zip code is an email address is phone is so let's try to i want the cash on delivery i have agreed with the terms and condition click on pay now and it says it is inserted now let's check on the user meta the data is inserted fine now we know the ide 37 already has the value this is the id so let's log out and login again this time the data is already present now instead of let's get into the user data will modify the name from will modify the name to my surname click on submit cash on delivery and i have accepted now it's again inserted let's check we don't want it to be inserted we want it to be updated so that means this query is not working properly actually we made a mistake this is the updated query so this should be updated p d a t u d and this should be inserted because this is the inside query we are writing here now let's try it's a 37 cash on delivery agree pay now now it should update let's check but the previous one preview previous one is getting removed it is because the value is not fetched and displayed now what we do is we have used select star and we'll use the mysql fetch array and in place of input field value will display all these values now we know the data is already updated over here we'll use this id and fetch all the values so for that we have the column call we have a function called mysql fetch underscore away association array so we have to get it from the result and here with this we can get the values php echo and then the column name the column name here is first name so before that we have to use if condition is it then only we have to we need to display it likewise we have to do this for all the input fields this is phone so for phone we have the corresponding column in for mobile and for email we don't have column so we'll leave that at this which we will be getting it from session we won't be updating it so if we wish we can remove this or else it will make some unnecessary errors for zip code we have zip in the as a column and now this is state again we don't have state column we have city address to address one then we have company name then we have last name first name so if i refresh align number 41 control g4 one column line number 122 if is set then echo unexpected curly brace i think we have to use this colon everywhere i'll pause this so now let's try to insert again to check and depend index date and email so as we know we don't have these two state if it is blank then we'll insert blank and where is email refresh cash on delivery i agree toms pay now updated let's check if it is updated even after refresh the data should persist and yes it is there so let's make it click on the updated one go to one two zero one four two one two zero one click on pay now yes it is updated apply it right one two three four five six click on okay we have not used this that's the reason it is showing error yes it is updated so after inserting or either updating what we need is we want the order data to be inserted the ordered product to be inserted on the table so for that we have to create a table the first one is order table which column six so the first one is the primary key and 11 we have to make this auto increment the second one is user id and again 11 the third one is total price the fourth one is time step we'll make the time step at the end then we have order status where care 255 then payment mode where care 255 then finally the timestamp date and time click on go maximum six payment mode okay we don't have to mention this actually it should be over here yes table is created now let's work on the next table order item primary key auto increment and this is the foreign key from table orders order orders yeah and 11 is also going to be 11. then we have product id this is again the product foreign key and then we have product price click on save now we will create this first we will focus on these two tables so we know after inserting an apps after updating we have to update the order table we will write here the relevant orders this is very careful i have mistakenly mistaken it click on save so it is updated let's get into the home and let's add some product to the cart so we have this so if we get into the checkout on the navbar control b it's inside inc nav so it's a button we'll make it as a link so if i click on checkout we are in the checkout now so i would like to check whether we are getting the values the cart values here so what i'll do is i'll echo the session cart [Applause] and to properly display i'll use pretag now if i refresh line number 82 id2 string conversion so what we'll do is we'll make the printer instead of echo so we have the id 8 with quantity 1 so okay when we are getting it now we would like to display these values just like we did on the nav bar we extract the id from the array start using forage blue so here it is so on the checkout will close the forage so we'll change the name to sql cart result underscore cart and a row underscore card so the reason for i'm doing this is i want to display the total amount over here so with the help of id so if you see on the session we are getting the id with the help of id i'm getting the products information the product information will definitely consist of a table name sorry price so i'll store it into a variable and use this variable has weighted it so this is from the for loop i'm storing it into a variable called total and we'll use this total over here php echo so it's a total again dot double hero or so now on line 261 we have we have missed some uh what do we say bracket so now we should see the total uh amount from the card 3700 so if i add some more let me add this 600 and let's get into check out so it would be the multiplication of for 3700 but it's not added to cart let's see why let's add this card i had to cut and remove this let's add some other add to cut jockey t-shirt i took our tire and solid t-shirt so we have two two products let's get right to check out this is not linked on the card.php control o card.php ctrl f check out this should be the link hf card.php it's checkout.php checkout.php and we should get the total price 1200 which is the addition of these two products so let me add the quantity add to cart let's make this quantity a bit more like three six centered into three one thousand eight hundred so on the checkout it should be two thousand four hundred three three three product of uh islands early and the one product of chalky so the total is what we are getting fine now we can see now the one thing what we have to do is i have to change this timestamp current timestamp and on order items i have to add one more column structure after product id go and it is going to be quantity 11 save fine and let us open these two side by side browse and browse it is because we are going to insert these two values once i click on checkout so on the checkout we have a message so in place of this line we will be inserting the values on both the tables comment now to insert on the first column we need the insert query so insert queries over here we'll copy the whole thing and paste it here so here the column name is going to be less and insert order this is a table name and the column name is user id total price order status which is a static word payment mode which you get from input field timestamp which is current timestamp we don't have to insert it so oh there's a problem here colonists double quotes is missing and again we have to pass the values as well so this is not going to be this long so the user id is we the user id which you will get from the session which is customer id cid the second thing total price which we have used for each let me see that everything is working fine remember this because it's over here and closes here now we have used forage somewhere we have to get that over here just copy it and paste it above so the total you will get it actually have to paste it over here the total will get it from here and then we have order status which is going to be static order placed and the payment board which you'll get from input type radio button once it is inserted order inserted so order will be inserted if this is inserted then we have to insert all the details into another another table the same way as we did here and insert order items the table name is order item order id so the column what we need is order id product id quantity product price so the order id which you will get after inserting this so we know we'll use this uh mysql underscore insert underscore id which we have used somewhere here so this is the place where we have used so that we have to get it and we will get to the order id dollar so we can all instead of we are we are assigning we can directly use this way and we'll pass this order id and the product id which we will get from the session in the for loop the form of for loop for each loop we don't need total so the product id can also be get from key and then we have quantity again it can be get from this for each qa quantity and then finally we have product price so we have to insert them individually so for that we have to write this code inside the for loop so finally product price on products table we have a column called price so we are uh the cart will have multiple product so each time it loops through it is going to insert one by one value into the table called order item so if this is set then we'll display echo saying insert it on both the tables like orders and order items now let's check my number 49 if line number 49 ctrl g4 9 if line number 63 okay copy copy type copy online number 75 it's a single we have already used over here we'll store this into a variable dollar price from product table dollar again we'll use the value quantity is a short form colon that seems okay now we have two products and let's check both the tables are empty so let's log out login from the graph we have already inserted it of users user data 37 on user 37 so this guy has the let me first log in 325 235 and only 235 it says it is undefined that means it is not able to fetch let's space this outside so i have placed this outside if i refresh the cart is not set so let's add some products add to cart check out undefined variable cart on line number 19 ctrl g online the session is not set it is because of this navbar which was missing fine now the total is also available so by default the data should fetch but it is not fetching let me see why so if you see it is inside the post let's make this outside and place it at the top over here copy it and paste it now let's refresh cid is not defined it's because it's from the session so replace at the extreme bottom over here cut it and reduces dollar underscore session customer id which is where we are getting session from and now if you see the data is fetched and if i try to update four to one two zero one which is actually my number cash on delivery click on pay now now data should be inserted inside on both tables let's check alan soli browse yes it is there browse yes it is there order place cash on delivery which we have made right now total price user id yes this is what we needed and it is working perfectly as we did it now let's try to insert let's try to log out and we'll log in with some other guy and this guy doesn't have the information we have to insert it phone number anything and present the card is zero cash on delivery let's check let's try to add some other guy the data would be inserted now let's order this phone check out now if you see data is filled now this time let's make this check yes let's check the second orders in the circuit here second roswell the second order is also inserted if you see here we are getting the message here but actually what it should happen what should happen is it should get into the uh myaccount.php which will display all the accounts so which will display all the orders which you have recently made so we have to redirect it that's it so the place where we have written the echo just comment it and write the code for redirection location my dot php it's my account on my account singular and we have to unset the session dollar underscore session which is cart fine once the data is inserted there is no point of having these item in the cart because we have already placed the order the same way once i get locked out i want to remove the cart log out fine let's try to we have already this guy in the order the list so let's add this check out cash on delivery and yes i agree with the terms and condition pay now cannot modify header order is sent line number 93 so instead of redirecting with the php why don't we go with javascript log out i have inserted some other values let me delete those check all delete check all delete duplicate and pin users data users data 35 and 37 let's get to 35 use this let's add some products check out data is already filled with the product check yes after clicking on pay now what should happen this session should be removed and data should be inserted inside the tables and it should redirect to not start php and yes it is working as we need it fine data is also inserted and it has been redirected to myaccount.php and here we will fetch the data and display it here
Info
Channel: UI MONK
Views: 7,208
Rating: undefined out of 5
Keywords:
Id: WG-1DAmRzQ4
Channel Id: undefined
Length: 59min 6sec (3546 seconds)
Published: Wed Nov 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.