Menu

Showing posts with label Shopping to Cart Using Mysql Php. Show all posts
Showing posts with label Shopping to Cart Using Mysql Php. Show all posts

Wednesday, October 8, 2014

Shopping to Cart Using Mysql Php

Shopping to Cart Using Mysql Php 

product.php

<?php session_start(); ?>
<html>
<head>
</head>
<body>
<?php
echo $_SESSION['username'];
$con=mysql_connect("localhost","root","");
mysql_select_db("test");
$sql="select * from product_item";
$nsql=mysql_query($sql);
?>
<table border="1">
<tr>
    <th>id</th>
    <th>name</th>
    <th>price</th>
    <th>img</th>
    <th>qty</th>
</tr>


<?php
while($row=mysql_fetch_array($nsql))
{
?>
<tr>
    <td><?php echo $row['p_id']; ?></td>
    <td><?php echo $row['p_name']; ?></td>
   
    <td><?php echo $row['p_price']; ?></td>
    <td><?php echo $row['p_img']; ?></td>
    <td><?php echo $row['p_qty']; ?></td>
    <td><?php $pid=$row['p_id'];
        echo "<a href='addtocart.php?id=$pid'>add to cart</a>"; ?></td>


</tr>
   
<?php
}
?>

</table>

</body>
</html>


addtocart.php

<?php
session_start();
$con=mysql_connect("localhost","root","");
mysql_select_db("test");

$ssql="select * from cart_detail where p_id=".$_GET['id']."";
$snrow=mysql_query($ssql);
$nrow=mysql_num_rows($snrow);
if($nrow>=1)
{
    $qsql=mysql_fetch_array($snrow);
    $qty=$qsql['qty']+1;
    echo $usql="update cart_detail set qty='$qty' where p_id=".$_GET['id']." and user_id='".$_SESSION['user']."'";
    mysql_query($usql);
}
if($nrow<=0)
{
    echo $acsql="insert into cart_detail(p_id,qty,user_id) values(".$_GET['id'].",'1','".$_SESSION['user']."')";
    mysql_query($acsql);

}


?>
<script language="JavaScript">
window.location="cart.php";

</script>

cart.php

<?php session_start(); ?>
<html>
<head>
</head>

<body>
<form method="post">
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("test");

if(isset($_GET['did']))
{
    mysql_query("delete from cart_detail where cart_id=".$_GET['did']." and  user_id='".$_SESSION['user']."'");
}

?>


<table border="1" align="center">
<tr>
    <td>Remove</td>
    <td>Id</td>
    <td>Product Name</td>
    <td>Price</td>
    <td>Quantity</td>
    <td>Total</td>
</tr>
<?php


$acsql="select * from cart_detail where user_id='".$_SESSION['user']."'";
$acnrow=mysql_query($acsql);
$tot=0;
$pname="";
while($acrow=mysql_fetch_array($acnrow))
{


    if(isset($_POST['upsubmit']))
    {
  
    $pid=$acrow['p_id'];
  
    echo $_POST[$pid];
    $qsql="update cart_detail set qty=".$_POST[$pid]." where p_id=$pid and user_id='".$_SESSION['user']."'";
    mysql_query($qsql);
    ?>
    <script language="JavaScript">
    window.location="cart.php";
    </script>
    <?php
    }



    $psql="select * from product_item where p_id=".$acrow['p_id']."";
    $pnrow=mysql_query($psql);
    $prow=mysql_fetch_array($pnrow);
  
  
  
  
?>
    <tr>
        <td><a href="cart.php?did=<?php echo $acrow['cart_id']; ?>">Delete</a></td>
        <td><?php echo $prow['p_id'];  ?></td>
        <td><?php echo $prow['p_name'];  ?>
        <?php
        $pname.=$prow['p_name'];
        ?>
        </td>
        <td><?php echo $prow['p_price'];  ?></td>
        <td><input type="text" name="<?php echo $prow['p_id']; ?>" value="<?php echo $acrow['qty'];  ?>" size="2"></td>
        <td><?php echo $prow['p_price']*$acrow['qty']; ?></td>
        <?php $tot=$tot+$prow['p_price']*$acrow['qty']; ?>
      
    </tr>

<?php

}
?>
<tr>
    <td colspan="6" align="right"><?php echo "Total Amount==".$tot; ?></td>
</tr>
<tr>
    <td colspan="6" align="center"><input type="submit" name="upsubmit" value="Update"></td>
  
</tr>
<tr>
    <td><a href="product.php">Back To Shopping</a></td>
</tr>
</table>

</form>
<form id="paypal_form" action="Payment.php" class="paypal" method="post">

    <input name="cmd" type="hidden" value="_xclick" />

    <input name="no_note" type="hidden" value="3" />


    <input type='hidden' name='currency_code' value='USD'>

   <input type="hidden" name="business" value="dadmom_1349350651_biz@ymail.com">

    <input name="currency_code" type="hidden" value="GBP" />

   <input name="bn" type="hidden" value="PP-BuyNowBF:btn_buynow_LG.gif:NonHostedGuest" />


  
    <input type="hidden" name="item_name" value="<?php echo $pname;  ?>" />
    <input type="hidden" name="amount" value="<?php  echo $tot;  ?>" />

    
   
    <table border="0" align="center">
    <tr>
    <td>
    <input type="hidden" name="return" value="http://localhost/index.php">
    <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
        <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
        </td>
    </tr>
</table>
</form>


</body>
</html>

payment.php

<?php
session_start();



// PayPal settings
$paypal_email = 'dadmom_1349350651_biz@ymail.com';
$return_url = 'http://localhost/paypal_/thanks.php';
$cancel_url = 'http://localhost/paypal_/index.php';
$notify_url = 'http://localhost/paypal_/index.php';

//$item_name = 'Test Item';
//$item_amount = $_SESSION['tot'];

// Include Functions
//include("functions.php");

//Database Connection


// Check if paypal request or response
if (!isset($_POST["txn_id"]) && !isset($_POST["txn_type"])){

    // Firstly Append paypal account to querystring
    $querystring .= "?business=".urlencode($paypal_email)."&";

    // Append amount& currency (£) to quersytring so it cannot be edited in html

    //The item name and amount can be brought in dynamically by querying the $_POST['item_number'] variable.
    $querystring .= "item_name=".urlencode($item_name)."&";
    $querystring .= "amount=".urlencode($item_amount)."&";

    //loop for posted values and append to querystring
    foreach($_POST as $key => $value){
        $value = urlencode(stripslashes($value));
        $querystring .= "$key=$value&";
    }

    // Append paypal return addresses
    $querystring .= "return=".urlencode(stripslashes($return_url))."&";
    $querystring .= "cancel_return=".urlencode(stripslashes($cancel_url))."&";
    $querystring .= "notify_url=".urlencode($notify_url);

    // Append querystring with custom field
    //$querystring .= "&custom=".USERID;

    // Redirect to paypal IPN
    header('location:https://www.sandbox.paypal.com/cgi-bin/webscr'.$querystring);
    exit();

}else{
    // Response from PayPal
}
?>

thanks.php

<?php
session_start();

$con=mysql_connect("localhost","root","");
mysql_select_db("test");

if(isset($_SESSION['uname']))
{
$asql="select * from cart_detail where user_id='".$_SESSION['uname']."'";

$anrow=mysql_query($asql);
while($arow=mysql_fetch_array($anrow))
{
    $dt=date("d-m-y");
    $psql="select * from product_item where p_id=".$arow['p_id']."";
    $pnrow=mysql_query($psql);
    $prow=mysql_fetch_array($pnrow);
    echo $osql="insert into order_detail(user_id,prd_id,qty,price,status,ord_date)
    values('".$_SESSION['uname']."',".$arow['p_id'].",".$arow['qty'].",".$prow['p_price'].",'padding','$dt')";
    mysql_query($osql);
    mysql_query("delete from cart_detail where user_id='".$_SESSION['uname']."'");
}
}
?>
<h2>Thank You For Shopping.....<?php echo $_SESSION['uname']; ?></h2>
    </div>
        </div>
      </div>


Database:

cart_detail









product_detail












order_detail





Step by Step demo of Shopping to Cart Using Mysql Php