How to write your own shopping cart for Ecommerce using core php & mysql:
Step1:
(1)Create table product :
create table product
(p_id int primary
key auto_increment,
p_name
varchar(200),
P_img varchar(200),
P_price
varchar(200));
(2)Create table mycart:
create table mycart
(c_id int
primary key auto_increment,
c_name varchar(200),
c_price varchar(200),
c_img varchar(200),
c_s_id varchar(200),
c_p_id varchar(200),
c_quantity varchar(200),
order_id varchar(200));
create table order
(id int primary key auto_increment ,
c_name
varchar(200),
c_price varchar(200),
c_img varchar(200),
c_quantity
varchar(200),
order_id
varchar(200),
c_p_id varchar(200),
o_s_id varchar(200),
b_fname
varchar(200),
b_name varchar(200));
Step 2:
Write code for “product.php”
which will display all record from
product table:
<?php
session_start();
if(isset($_SESSION['loginuser-id'])) {
$s_id
=$_SESSION['loginuser-id'];
}
else {
$s_id1 =
session_id();
}
?>
<?php
include("connect.php");
$sql="select * from product";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result))
{
$id=$row['p_id'];
$name=$row['p_name'];
$image=$row['p_img'];
$price=$row['p_price'];
echo "product name" ;
echo $name ;
echo "product image ";
echo "<img
src='upload/".$row['p_img']."' height=200 width=200 border=10 >";
echo
"product price";
echo $price;
echo '
<select name=c_quantity>
<option value=1>1</option>
<option value=2>2</option>
</select>';
?>
<a href="view123.php?id=<?php echo
$row['p_id']; ?>">
<?php echo
$row['p_id']; ?>
<input type=submit
name=submit value=Buy-Now>
</a>
<?php
}
?>
Step 3:
Write code for “view123.php”
file which will display selected product item from product table:
<?php
session_start();
if(isset($_SESSION['loginuser-id'])) {
$s_id
=$_SESSION['loginuser-id'];
}
else {
$s_id1 =
session_id();
}
?>
<?php
$id=$_GET['id'];
echo '$id';
include("connect.php");
$sql="select * from product where p_id='$id'";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result))
{
$id=$row['p_id'];
$name=$row['p_name'];
$image=$row['p_img'];
$price=$row['p_price'];
echo "product name" ;
echo $name ;
echo "product image ";
echo "<img
src='upload/".$row['p_img']."' height=200 width=200 border=10 >";
echo
"product price";
echo $price;
echo '
<form action=addcart1.php
method=post>
<select name=c_quantity>
<option value=1>1</option>
<option value=2>2</option>
</select>
<input type=hidden
name=hidden value="'.$row['p_id'].'">
<input type=submit
name=submit value=Add-cart>
</form>
';
}
?>
Step 4:
Write code for file "addcart1.php" which will add details to “mycart” table
means it is used for add
cart functionality:
<?php
include("connect.php");
session_start();
if(isset($_SESSION['loginuser-id'])) {
$s_id1
=$_SESSION['loginuser-id'];
}
else {
$s_id1
=session_id();
}
if(isset($_POST['submit']))
{
include("connect.php");
$id=$_POST['hidden'];
echo $id;
$quantity=$_POST['c_quantity'];
$sql1="select * from product where p_id='$id'";
$result=mysql_query($sql1) or die(mysql_error());
while($row=mysql_fetch_array($result))
{
$p_id=$row['p_id'];
$p_name=$row['p_name'];
echo $p_name;
$p_img=$row['p_img'];
echo $p_img;
$p_price=$row['p_price'];
echo $p_price;
}
$sql2="insert into
mycart(c_name,c_price,c_img,c_s_id,c_quantity,c_p_id,order_id)values('$p_name','$p_price','$p_img','$s_id1','$quantity','$p_id','$s_id1')";
echo $sql2;
mysql_query($sql2) or die (mysql_error());
header("Location:viewcart.php");
}
?>
Step 5:
Write code for “viewcart.php”
file which will display details of product which is added to cart (mycart table):
<?php
session_start();
if(isset($_SESSION['loginuser-id'])) {
$s_id1
=$_SESSION['loginuser-id'];
}
else {
$s_id1
=session_id();
}
include("connect.php");
$sql3="select c_id,
c_name,c_price,c_img,SUM(c_quantity) from
mycart where c_s_id='$s_id1'
group by c_p_id";
echo $sql3;
$result1=mysql_query($sql3) or die(mysql_error());
while($row1=mysql_fetch_array($result1))
{
$sum=0;
$x=$row1['SUM(c_quantity)'];
echo '<br>';
$sub=$x*$row1['c_price'];
echo '<br>';
$sum=$sum+$sub;
echo '<table border=1>
<tr><td align=center>PRODUCT-NAME</td>
<td align=center>PRODUCT-PRICE</td>
<td align=center>PRODUCT</td>
<td align=center>PRODUCT-QUANTITY</td>
<td align=center>TOTAL-PRICE</td>
<td></td>
<td></td>
</tr>';
echo '<tr>';
echo '<td align=center>';
echo $row1['c_name'];
echo '<td align=center>';
echo $row1['c_price'];
echo '</td>';
echo '<td align=center>';
echo "<img
src=upload/".$row1['c_img']."
height=200 width=200 border=10
>";
echo '</td>';
echo '<td align=center>';
echo $x;
echo '</td>';
echo '<td align=center>';
echo $sub;
echo '</td>';
echo '<td
align=center>';
echo '<form action=delcart.php method=post>
<input type=hidden name=hidden value="'.$row1['c_id'].'">
<input type=submit
name=delcart
value="delete-cart"></form>';
echo '</td>';
echo '<td align=center>';
echo '<a
href="product.php"><button>continue
shopping</button></a>';
echo '</td></tr>';
echo '</table>';
}
?>
<body>
<div id="clicker" >
<button>Place
your order!</button>
</div>
<div
id="popup-wrapper">
<a
href="#" id="close-btn"><font
cloro=red>Close(X)</font></a>
<table
border=0>
<tr><td>
<form
action=register.php method=post>
<table
border=0>
<tr><td>
firstname</td><td>
<input type=text name=fname></td></tr>
<tr><td>
lastname</td><td><input
type=text name=lname></td></tr>
<tr><td>
emailid</td><td><input
type=text name=email_id></td></tr>
<tr><td>
password</td><td><input type=password name=password></td></tr>
<tr><td>
password</td><td><input
type=password name=cpassword></td></tr>
<tr><td></td><td>
<input type=submit
name=submit> </td></tr>
</table></form></td>
<td></td><td>
<form action=login.php
method=post>
<table border=0 bgcolor=red>
<tr><td>Registered user make Login
here</td><td></td></tr>
<tr><td>Email-id</td><td><input
type=text name=email_id></td></tr>
<tr><td>Password</td><td><input
type=password name=password></td></tr>
<tr><td></td><td><input
type=submit name=submit value=Login></td></tr>
</form>
</table>
</td><td></td>
</tr></table>
</div>
</body>
</html>
Step 6:
Now write code for “delcart.php” Deleting cart which is added to “mycart” table:
<?php
session_start();
if(isset($_SESSION['loginuser-id'])) {
$s_id1
=$_SESSION['loginuser-id'];
}
else {
$s_id1
=session_id();
}
$id=$_POST['hidden'];
echo $id;
include("connect.php");
$sql="delete from mycart where c_id='$id'";
mysql_query($sql);
header("Location:viewcart.php");
?>
Step 7:
Write code for order form:
<?php
session_start();
if(isset($_SESSION['email_id']))
{
echo '
<form action="ship2.php" method=post>
name<input type=text name=b_name>
lname<input type=text
name=b_fname>
<input type=submit
name=submit name=place-order>
</form>';
}
else
{
header("Location:eroor.php");
}
?>
<html>
<body>
<a href="logout.php">Logout</a>
</body>
</html>
Step 8:
Now write code for file "ship2.php" placing order & storing order
details into order table:
<?php
session_start();
if(isset($_SESSION['loginuser-id']))
{
$s_id1
=$_SESSION['loginuser-id'];
}
else {
$s_id1 =
session_id();
}
error_reporting(0);
if(isset($_POST['submit']))
{
include("connect.php");
$b_name=$_POST["b_name"];
$b_fname=$_POST["b_fname"];
$sql1="select
* from mycart where c_s_id='$s_id1'";
$res1=
mysql_query($sql1);
while($result1=
mysql_fetch_array($res1))
{
echo $C_name=$result1["c_name"];
echo
$C_price=$result1["c_price"];
echo
$C_img=$result1["c_img"];
echo
$C_quantity=$result1["c_quantity"];
echo
Order;
echo
$ord_id=$result1["order_id"];
echo
$C_p_id=$result1["c_p_id"];
//$sql="insert
into
order(c_name,c_price,c_img,c_quantity,order_id,c_p_id,o_s_id,b_name,b_fname)
//values('$C_name','$C_price','$C_img','$C_quantity','$ord_id','$C_p_id','$s_id1','$b_name','$b_fname')";
$sql="INSERT
INTO `ecommerce`.`order` (`c_name`, `c_price`, `c_img`, `c_quantity`,
`order_id`, `c_p_id`, `o_s_id`, `b_name`, `b_fname`) VALUES ('$C_name',
'$C_price', '$C_img', '$C_quantity', '$ord_id', '$C_p_id', '$s_id1 ', '$b_name',
'$b_fname')";
//echo
$sql;
mysql_query($sql)
or die(mysql_error());
}
}
?>
No comments:
Post a Comment