I am working on Ecommerce website project. Whenever I logged in to my website and redirected to products.php page where all products are listed on page and after i tries to add the products to cart by clicking add to cart button a new page is redirected and showing an error: “Incorrect integer value: ‘’ for column ‘user_id’ at row 1”
here is my code for adding product to cart i.e. cart-add.php:
[php]<?php
session_start();
error_reporting(0);
require(“includes/common.php”);
if (isset($_GET[‘id’]) && is_numeric($_GET[‘id’])) {
$item_id = $_GET[‘id’];
$user_id = $_SESSION[‘user_id’];
$query = “INSERT INTO users_items(user_id, item_id, status) VALUES(’$user_id’, ‘$item_id’, ‘Added to cart’)”;
mysqli_query($con, $query) or die(mysqli_error($con));
header(‘location: products.php’);
}
?> [/php]
here is the code for products listed page from where user can add the product to cart. products.php:
[php]<?php
session_start();
require(“includes/common.php”);
if (!isset($_SESSION[‘email’]))
header(‘location: index.php’);
?>
<?php include 'includes/header.php'; include 'includes/check-if-added.php'; ?>Cannon EOS
Price: Rs. 36000
<?php if (!isset($_SESSION['email'])) { ?> <?php } else { // 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 'Added to cart'; } else { ?> Add to cart <?php } } ?> <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 {
// 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 {
// 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 {
// 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 {
//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 {
// 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 {
// 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 {
// 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 {
// 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 {
//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 {
// 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 {
// 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]
here is the code for checking if the products is added to cart or not. check-if-added.php:
[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 item_id=’$item_id’ AND user_id =’$user_id’ and status=‘Added to cart’”;
$result = mysqli_query($con, $query) or die(mysqli_error($con));
if (mysqli_num_rows($result) >= 1) {
return 1;
} else {
return 0;
}
}
?>[/php]