0

I created an add to cart function but not working properly
on submit i see these errors on the place of product details

Notice: Undefined variable: product_id in C:\wamp\www\connectdemo\product_details.php on line 218

etc etc....

product_details.php

     <?php
    session_start();    
    include_once("includes/layout/header.php");
    include_once("includes/connection.php");
    include_once("includes/functions.php");

    if(isset($_POST["addtocart"])) {
        $pid        = $_POST["productid"];
        $prod_name  = $_POST["productname"];
        $prod_price = $_POST["productprice"];

        if(isset($_POST['command']) == 'add' && $_POST['productid']>0 ) {
            $pid=$_POST['productid'];
            echo addtocart($pid,1, $prod_name, $prod_price);
            //exit();
        }
    }

?>

functions.php

    function addtocart($pid,$q,$pname,$pprice){
            if($pid<1 or $q<1) return;

    if(is_array($_SESSION['cart'])){
        if(product_exists($pid)) return;
        $max=count($_SESSION['cart']);
        $_SESSION['cart'][$max]['productid']=$pid;
        $_SESSION['cart'][$max]['qty']=$q;
    }
    else{
        $_SESSION['cart']=array();
        $_SESSION['cart'][0]['productid']=$pid;
        $_SESSION['cart'][0]['qty']=$q;
    }

    header("location:cart.php?pname=$pname&price=$pprice");
}
2
Contributors
2
Replies
13
Views
2 Years
Discussion Span
Last Post by cereal
0

Use your editor to find the string product_id. Looking at your pasted code I don't see it, but probably somewhere you have $_POST['product_id'] instead of $_POST['productid'], or set by another array.

To avoid these kind of problems use array_key_exists('key_to_verify', $array):

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.