Create Shopping Cart Using PHP and Mysql


Create Shopping Cart Using PHP and Mysql
Hello Friends,
In this Video We are creating Simple Php Cart Tutorial.

Download Sorce File 🙂

Create new shopping cart project and add four product images.
Create new database Product detail,
create a new table product and add 4 column id, pname , images and price.
now we are inserting our product details in our table we are adding four product in table.

before we start make sure that you had subscribe to daily tuition channel.
First Create a new php file with php extenstion.
We are using bootstrap framework with javascript library.
Copy bootstrap cdn in your cart.php file in head section.
Now we are import google font in for better font family.
Copy google font url and paste in style tag in our file.
Now create new php session on top of the page.
And create a new variable and assign database name to that variable.
Now create Database connection and pass four parameter .
Host name, username, password and database name.

Now create sql query to fetch data from our table.

Execute mysql query with two parameter connection and Query string.
Now we are check if product table number of rows is greater than 0.

In while condition we are fetch array and store in $row variable in array format.
Now we are display dynamic images so we are adding php tag in source of image tag.

This way we are display Dynamic image on our page.
Now I want to display product name php $row[“pname”]
in this way we are display product name from sql table.
Do the same to display price also.
Now add input type text to add quantity of product
Now create submit button input type=”submit”
Add css style to division tag of product class.
Now create a table to display product details.
Create a php code to calculate total amount of product.
So first we are check that our cart session is empty or not if it is not empty

To display product information in key and value pairs. This way we can access all values of session cart.
Now I want to display item name so is have use echo
To display item name from our cart session
Now I want to display item quantity and price so I do that same as I do this before.
After this we want to calculate price of product base on quanitity.
So we are adding item quantity divide by item price.
After this we want to remove item from cart so we are add

to access the id of product.
Now we want to calculate total price of product so we add

After this create new row with colspan three and align this to right.
Now display total amount of your product with

after this create new css style to table table head and table data.
And also style up shopping cart details titile.
So now run script
We are getting error in the line no 137
So make sure that you are compete all curly brackets.
Here we miss one curly bracket so we add this to remove this error and again run this project.
Now we cannot see the image because we only specify the name of our image not the absolute path of our image so I have move this image from image folder to our root path. And delete image folder.
Now we are creating a php code top of the page

Then check that our session cart variable value is set or not with isset function.
Create new variable item array id with two parameters.

Now add in array function to check whether a value exists or not.
Create item array variable
Now after this assign item array value to session cart to count index we already specify session cart to count variable.
Now redirect our page with echo window.location=cart.php

And redirect page to window location = cart.php page.
After this check if session cart variable is not set then the isset execute else block.

Now we will run our project but we find that table is not display properly so let’s find out what is the problem
In this code we missing the table tag so we will insert table tag with table and table bordered.
Then we are check that if get action is equal to delete if it is true then further code is execute.
Now create a for each to access the value of session cart variable.
After that check that if product if is equal to get id.
If it is true then execute further code and unset session cart variable.
Now we cannot see product price and name so let’s find out what is the problem.
We are missing the echo function in the h5 tag in design section.

Add laptop bag one quantity and vivo v5 with three quantity.
Iphone 6 with 2 quantity and Phillips trimmer with one quantity.
And at the bottom you will see the total price is calculated.
Now we are remove vivo v5 item from the cart and it was removed.
At again remove iphone 6 and you will see the difference in total price is reduced because we was removed one product.

so if you have any question or any opinion please comment on my video and share this video to you friends. If you have any other question you can chat with me in channel discussion. You can follow me on Facebook Page.

Thank You…!