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");
}

Recommended Answers

All 2 Replies

no one here to help me out

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):

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.