getting Undefined Index error


#1

I am working on ecommerce website. Whenever I logged in to website and redirects to products.php page where products is listed, an error ‘Undefined Index: user_id’ occurs in checking the products been added or not added to cart.

here the error occurs in line 4 of check-if-added.php, code is given below.

[php]<?php
//This code checks if the product is added to cart.
function check_if_added_to_cart($item_id) {
$user_id = $_SESSION[‘user_id’]; //We’ll get the user_id from the session
require(“includes/common.php”);
// We will select all the entries from the user_items table where the item_id is equal to the item_id we passed to this function, user_id is equal to the user_id in the session and status is ‘Added to cart’
$query = “SELECT * FROM users_items WHERE user_id =’$user_id’ AND item_id=’$item_id’ and status=‘Added to cart’”;
$result = mysqli_query($con, $query) or die(mysqli_error($con));
while($row = mysqli_fetch_array($result)){
if (mysqli_num_rows($result) >= 1) {
return 1;
} else {
return 0;
}
}
}

?>[/php]

and the code for products.php page where the products is listed :

[php]<?php
session_start();
require(“includes/common.php”);
if (!isset($_SESSION[‘email’]))
header(‘location: index.php’);

?>

Products | BookMyKart
<body>
    <?php
    include 'includes/header.php';
    include 'includes/check-if-added.php';
    ?>
    <div class="container" id="content">
        <!-- Jumbotron Header -->
        <div class="jumbotron home-spacer" id="products-jumbotron">
            <center><h1 style="letter-spacing: 3px;">Welcome to our Online Store!</h1></center>
            <p>We have the best cameras, watches and shirts for you. No need to hunt around, we have all in one place.</p>

        </div>
        <hr>

        <div class="row text-center" id="cameras">
            <div class="col-md-3 col-sm-6 home-feature">
                <div class="thumbnail">
                    <img src="img/5.jpg" alt="">
                    <div class="caption">
                        <h3>Cannon EOS </h3>
                        <p>Price: Rs. 36000 </p>
                        <?php if (!isset($_SESSION['email'])) { ?>
                            <p><a href="#myModal" role="button" data-toggle="modal" class="btn btn-primary btn-block">Buy Now</a></p>
                            <?php
                        } else {
                            //We have created a function to check whether this particular product is added to cart or not.
                            if (check_if_added_to_cart(1)) { //This is same as if(check_if_added_to_cart != 0)
                                echo '<a href="#" class="btn btn-block btn-success" disabled>Added to cart</a>';
                            } else {
                                ?>
                                <a href="cart-add.php?id=1" name="add" value="add" class="btn btn-block btn-primary">Add to cart</a>
                                <?php
                            }
                        }
                        ?>
                    </div>
                </div>
            </div>

            <div class="col-md-3 col-sm-6 home-feature">
                <div class="thumbnail">
                    <img src="img/2.jpg" alt="">
                    <div class="caption">
                        <h3>Nikon EOS </h3>
                        <p>Price: Rs. 40000</p>
                        <?php if (!isset($_SESSION['email'])) { ?>
                            <p><a href="#myModal" role="button" data-toggle="modal" class="btn btn-primary btn-block">Buy Now</a></p>
                            <?php
                        } else {
                            //We have created a function to check whether this particular product is added to cart or not.
                            if (check_if_added_to_cart(2)) { //This is same as if(check_if_added_to_cart != 0)
                                echo '<a href="#" class="btn btn-block btn-success" disabled>Added to cart</a>';
                            } else {
                                ?>
                                <a href="cart-add.php?id=2" name="add" value="add" class="btn btn-block btn-primary">Add to cart</a>
                                <?php
                            }
                        }
                        ?>
                        </a>
                    </div>
                </div>
            </div>

            <div class="col-md-3 col-sm-6 home-feature">
                <div class="thumbnail">
                    <img src="img/3.jpg" alt="">
                    <div class="caption">
                        <h3>Sony DSLR</h3>
                        <p>Price: Rs. 50000</p>
                        <?php if (!isset($_SESSION['email'])) { ?>
                            <p><a href="#myModal" role="button" data-toggle="modal" class="btn btn-primary btn-block">Buy Now</a></p>
                            <?php
                        } else {
                            //We have created a function to check whether this particular product is added to cart or not.
                            if (check_if_added_to_cart(3)) { //This is same as if(check_if_added_to_cart != 0)
                                echo '<a href="#" class="btn btn-block btn-success" disabled>Added to cart</a>';
                            } else {
                                ?>
                                <a href="cart-add.php?id=3" name="add" value="add" class="btn btn-block btn-primary">Add to cart</a>
                                <?php
                            }
                        }
                        ?>
                        </a>
                    </div>
                </div>
            </div>

            <div class="col-md-3 col-sm-6 home-feature">
                <div class="thumbnail">
                    <img src="img/4.jpg" alt="">
                    <div class="caption">
                        <h3>Olympus DSLR</h3>
                        <p>Price: Rs. 50000</p>
                        <?php if (!isset($_SESSION['email'])) { ?>
                            <p><a href="#myModal" role="button" data-toggle="modal" class="btn btn-primary btn-block">Buy Now</a></p>
                            <?php
                        } else {
                            //We have created a function to check whether this particular product is added to cart or not.
                            if (check_if_added_to_cart(4)) { //This is same as if(check_if_added_to_cart != 0)
                                echo '<a href="#" class="btn btn-block btn-success" disabled>Added to cart</a>';
                            } else {
                                ?>
                                <a href="cart-add.php?id=4" name="add" value="add" class="btn btn-block btn-primary">Add to cart</a>
                                <?php
                            }
                        }
                        ?>
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <div class="row text-center" id="watches">
            <div class="col-md-3 col-sm-6 home-feature">
                <div class="thumbnail">
                    <img src="img/18.jpg" alt="">
                    <div class="caption">
                        <h3>Titan Model1</h3>
                        <p>Price: Rs. 13000</p>
                        <?php if (!isset($_SESSION['email'])) { ?>
                            <p><a href="#myModal" role="button" data-toggle="modal" class="btn btn-primary btn-block">Buy Now</a></p>
                            <?php
                        } else {
                            //We have created a function to check whether this particular product is added to cart or not.
                            if (check_if_added_to_cart(5)) { //This is same as if(check_if_added_to_cart != 0)
                                echo '<a href="#" class="btn btn-block btn-success" disabled>Added to cart</a>';
                            } else {
                                ?>
                                <a href="cart-add.php?id=5" name="add" value="add" class="btn btn-block btn-primary">Add to cart</a>
                                <?php
                            }
                        }
                        ?>
                        </a>
                    </div>
                </div>
            </div>

            <div class="col-md-3 col-sm-6 home-feature">
                <div class="thumbnail">
                    <img src="img/19.jpg" alt="">
                    <div class="caption">
                        <h3>Titan Model2</h3>
                        <p>Price: Rs. 3000</p>
                        <?php if (!isset($_SESSION['email'])) { ?>
                            <p><a href="#myModal" role="button" data-toggle="modal" class="btn btn-primary btn-block">Buy Now</a></p>
                            <?php
                        } else {
                            //We have created a function to check whether this particular product is added to cart or not.
                            if (check_if_added_to_cart(6)) { //This is same as if(check_if_added_to_cart != 0)
                                echo '<a href="#" class="btn btn-block btn-success" disabled>Added to cart</a>';
                            } else {
                                ?>
                                <a href="cart-add.php?id=6" name="add" value="add" class="btn btn-block btn-primary">Add to cart</a>
                                <?php
                            }
                        }
                        ?>
                        </a>
                    </div>
                </div>
            </div>

            <div class="col-md-3 col-sm-6 home-feature">
                <div class="thumbnail">
                    <img src="img/20.jpg" alt="">
                    <div class="caption">
                        <h3>HMT Milan</h3>
                        <p>Price: Rs. 8000 </p>
                        <?php if (!isset($_SESSION['email'])) { ?>
                            <p><a href="#myModal" role="button" data-toggle="modal" class="btn btn-primary btn-block">Buy Now</a></p>
                            <?php
                        } else {
                            //We have created a function to check whether this particular product is added to cart or not.
                            if (check_if_added_to_cart(7)) { //This is same as if(check_if_added_to_cart != 0)
                                echo '<a href="#" class="btn btn-block btn-success" disabled>Added to cart</a>';
                            } else {
                                ?>
                                <a href="cart-add.php?id=7" name="add" value="add" class="btn btn-block btn-primary">Add to cart</a>
                                <?php
                            }
                        }
                        ?>
                        </a>
                    </div>
                </div>
            </div>

            <div class="col-md-3 col-sm-6 home-feature">
                <div class="thumbnail">
                    <img src="img/21.jpg" alt="">
                    <div class="caption">
                        <h3>Faber Luba </h3>
                        <p>Price: Rs. 18000 </p>
                        <?php if (!isset($_SESSION['email'])) { ?>
                            <p><a href="#myModal" role="button" data-toggle="modal" class="btn btn-primary btn-block">Buy Now</a></p>
                            <?php
                        } else {
                            //We have created a function to check whether this particular product is added to cart or not.
                            if (check_if_added_to_cart(8)) { //This is same as if(check_if_added_to_cart != 0)
                                echo '<a href="#" class="btn btn-block btn-success" disabled>Added to cart</a>';
                            } else {
                                ?>
                                <a href="cart-add.php?id=8" name="add" value="add" class="btn btn-block btn-primary">Add to cart</a>
                                <?php
                            }
                        }
                        ?>
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <div class="row text-center" id="shirts">
            <div class="col-md-3 col-sm-6 home-feature">
                <div class="thumbnail">
                    <img src="img/22.jpg" alt="">
                    <div class="caption">
                        <h3>H&W </h3>
                        <p>Price: Rs. 800 </p>
                        <?php if (!isset($_SESSION['email'])) { ?>
                            <p><a href="#myModal" role="button" data-toggle="modal" class="btn btn-primary btn-block">Buy Now</a></p>
                            <?php
                        } else {
                            //We have created a function to check whether this particular product is added to cart or not.
                            if (check_if_added_to_cart(9)) { //This is same as if(check_if_added_to_cart != 0)
                                echo '<a href="#" class="btn btn-block btn-success" disabled>Added to cart</a>';
                            } else {
                                ?>
                                <a href="cart-add.php?id=9" name="add" value="add" class="btn btn-block btn-primary">Add to cart</a>
                                <?php
                            }
                        }
                        ?>
                        </a>
                    </div>
                </div>
            </div>

            <div class="col-md-3 col-sm-6 home-feature">
                <div class="thumbnail">
                    <img src="img/23.jpg" alt="">
                    <div class="caption">
                        <h3>Luis Phil</h3>
                        <p>Price: Rs. 1000</p>
                        <?php if (!isset($_SESSION['email'])) { ?>
                            <p><a href="#myModal" role="button" data-toggle="modal" class="btn btn-primary btn-block">Buy Now</a></p>
                            <?php
                        } else {
                            //We have created a function to check whether this particular product is added to cart or not.
                            if (check_if_added_to_cart(10)) { //This is same as if(check_if_added_to_cart != 0)
                                echo '<a href="#" class="btn btn-block btn-success" disabled>Added to cart</a>';
                            } else {
                                ?>
                                <a href="cart-add.php?id=10" name="add" value="add" class="btn btn-block btn-primary">Add to cart</a>
                                <?php
                            }
                        }
                        ?>
                        </a>
                    </div>
                </div>
            </div>

            <div class="col-md-3 col-sm-6 home-feature">
                <div class="thumbnail">
                    <img src="img/24.jpg" alt="">
                    <div class="caption">
                        <h3>John Zok</h3>
                        <p>Price: Rs. 1500</p>
                        <?php if (!isset($_SESSION['email'])) { ?>
                            <p><a href="#myModal" role="button" data-toggle="modal" class="btn btn-primary btn-block">Buy Now</a></p>
                            <?php
                        } else {
                            //We have created a function to check whether this particular product is added to cart or not.
                            if (check_if_added_to_cart(11)) { //This is same as if(check_if_added_to_cart != 0)
                                echo '<a href="#" class="btn btn-block btn-success" disabled>Added to cart</a>';
                            } else {
                                ?>
                                <a href="cart-add.php?id=11" name="add" value="add" class="btn btn-block btn-primary">Add to cart</a>
                                <?php
                            }
                        }
                        ?>
                        </a>
                    </div>
                </div>
            </div>

            <div class="col-md-3 col-sm-6 home-feature">
                <div class="thumbnail">
                    <img src="img/25.jpg" alt="">
                    <div class="caption">
                        <h3>Jhalsani</h3>
                        <p>Price Rs. 1300</p>
                        <?php if (!isset($_SESSION['email'])) { ?>
                            <p><a href="#myModal" role="button" data-toggle="modal" class="btn btn-primary btn-block">Buy Now</a></p>
                            <?php
                        } else {
                            //We have created a function to check whether this particular product is added to cart or not.
                            if (check_if_added_to_cart(12)) { //This is same as if(check_if_added_to_cart != 0)
                                echo '<a href="#" class="btn btn-block btn-success" disabled>Added to cart</a>';
                            } else {
                                ?>
                                <a href="cart-add.php?id=12" name="add" value="add" class="btn btn-block btn-primary">Add to cart</a>
                                <?php
                            }
                        }
                        ?>
                        </a>
                    </div>
                </div>
            </div>

        </div>
        <hr>
    </div>

    <?php include("includes/footer.php"); ?>
	<?php include("modal.php"); ?>
</body>
[/php]

#2

Your session does not contain a user_id key. Add the code where you set the session data (login) if you want more feedback.

You should be using prepared and parameterized queries to avoid sql injections hacks.