I’m trying to use one of the shopping cart tutorial, everything works just fine, but if product (SKU) is numeric only it does not work. Can anyone help me with this case?
I got this tutorial from source: https://www.allphptricks.com/simple-shopping-cart-using-php-and-mysql/
P.S. If I add any letter to sku for example: k00199288, it works just fine, but if I leave sku in format: 00199288, it does not see sku.
Here is the source code:
<code><?php
session_start();
include('../admin/config.php');
$status="";
if (isset($_POST['sku']) && $_POST['sku']!=""){
$code = $_POST['sku'];
$result = mysqli_query(
$con,
"SELECT * FROM `products` WHERE `sku`='$sku'"
);
$row = mysqli_fetch_assoc($result);
$name = $row['name'];
$sku = $row['sku'];
$price = $row['price'];
$image = $row['image'];
$cartArray = array(
$sku=>array(
'name'=>$name,
'sku'=>$sku,
'price'=>$price,
'quantity'=>1,
'image'=>$image)
);
if(empty($_SESSION["shopping_cart"])) {
$_SESSION["shopping_cart"] = $cartArray;
$status = "<div class='box'>Product is added to your cart!</div>";
}else{
$array_keys = array_keys($_SESSION["shopping_cart"]);
if(in_array($sku,$array_keys)) {
$status = "<div class='box' style='color:red;'>
Product is already added to your cart!</div>";
} else {
$_SESSION["shopping_cart"] = array_merge(
$_SESSION["shopping_cart"],
$cartArray
);
$status = "<div class='box'>Product is added to your cart!</div>";
}
}
}
?>
<?php
if(!empty($_SESSION["shopping_cart"])) {
$cart_count = count(array_keys($_SESSION["shopping_cart"]));
?>
<div class="cart_div">
<a href="cart.php"><img src="cart-icon.png" /> Cart<span>
<?php echo $cart_count; ?></span></a>
</div>
<?php
}
?>
<?php
$result = mysqli_query($con,"SELECT * FROM `products`");
while($row = mysqli_fetch_assoc($result)){
echo "<div class='product_wrapper'>
<form method='post' action=''>
<input type='hidden' name='sku' value=".$row['sku']." />
<div class='image'><img src='".$row['image']."' /></div>
<div class='name'>".$row['name']."</div>
<div class='price'>$".$row['price']."</div>
<button type='submit' class='buy'>Buy Now</button>
</form>
</div>";
}
mysqli_close($con);
?>
<div style="clear:both;"></div>
<div class="message_box" style="margin:10px 0px;">
<?php echo $status; ?>
</div>
</code>
<code><?php
session_start();
include('../admin/config.php');
$status="";
if (isset($_POST['sku']) && $_POST['sku']!=""){
$code = $_POST['sku'];
$result = mysqli_query(
$con,
"SELECT * FROM `products` WHERE `sku`='$sku'"
);
$row = mysqli_fetch_assoc($result);
$name = $row['name'];
$sku = $row['sku'];
$price = $row['price'];
$image = $row['image'];
$cartArray = array(
$sku=>array(
'name'=>$name,
'sku'=>$sku,
'price'=>$price,
'quantity'=>1,
'image'=>$image)
);
if(empty($_SESSION["shopping_cart"])) {
$_SESSION["shopping_cart"] = $cartArray;
$status = "<div class='box'>Product is added to your cart!</div>";
}else{
$array_keys = array_keys($_SESSION["shopping_cart"]);
if(in_array($sku,$array_keys)) {
$status = "<div class='box' style='color:red;'>
Product is already added to your cart!</div>";
} else {
$_SESSION["shopping_cart"] = array_merge(
$_SESSION["shopping_cart"],
$cartArray
);
$status = "<div class='box'>Product is added to your cart!</div>";
}
}
}
?>
<?php
if(!empty($_SESSION["shopping_cart"])) {
$cart_count = count(array_keys($_SESSION["shopping_cart"]));
?>
<div class="cart_div">
<a href="cart.php"><img src="cart-icon.png" /> Cart<span>
<?php echo $cart_count; ?></span></a>
</div>
<?php
}
?>
<?php
$result = mysqli_query($con,"SELECT * FROM `products`");
while($row = mysqli_fetch_assoc($result)){
echo "<div class='product_wrapper'>
<form method='post' action=''>
<input type='hidden' name='sku' value=".$row['sku']." />
<div class='image'><img src='".$row['image']."' /></div>
<div class='name'>".$row['name']."</div>
<div class='price'>$".$row['price']."</div>
<button type='submit' class='buy'>Buy Now</button>
</form>
</div>";
}
mysqli_close($con);
?>
<div style="clear:both;"></div>
<div class="message_box" style="margin:10px 0px;">
<?php echo $status; ?>
</div>
</code>
<?php
session_start();
include('../admin/config.php');
$status="";
if (isset($_POST['sku']) && $_POST['sku']!=""){
$code = $_POST['sku'];
$result = mysqli_query(
$con,
"SELECT * FROM `products` WHERE `sku`='$sku'"
);
$row = mysqli_fetch_assoc($result);
$name = $row['name'];
$sku = $row['sku'];
$price = $row['price'];
$image = $row['image'];
$cartArray = array(
$sku=>array(
'name'=>$name,
'sku'=>$sku,
'price'=>$price,
'quantity'=>1,
'image'=>$image)
);
if(empty($_SESSION["shopping_cart"])) {
$_SESSION["shopping_cart"] = $cartArray;
$status = "<div class='box'>Product is added to your cart!</div>";
}else{
$array_keys = array_keys($_SESSION["shopping_cart"]);
if(in_array($sku,$array_keys)) {
$status = "<div class='box' style='color:red;'>
Product is already added to your cart!</div>";
} else {
$_SESSION["shopping_cart"] = array_merge(
$_SESSION["shopping_cart"],
$cartArray
);
$status = "<div class='box'>Product is added to your cart!</div>";
}
}
}
?>
<?php
if(!empty($_SESSION["shopping_cart"])) {
$cart_count = count(array_keys($_SESSION["shopping_cart"]));
?>
<div class="cart_div">
<a href="cart.php"><img src="cart-icon.png" /> Cart<span>
<?php echo $cart_count; ?></span></a>
</div>
<?php
}
?>
<?php
$result = mysqli_query($con,"SELECT * FROM `products`");
while($row = mysqli_fetch_assoc($result)){
echo "<div class='product_wrapper'>
<form method='post' action=''>
<input type='hidden' name='sku' value=".$row['sku']." />
<div class='image'><img src='".$row['image']."' /></div>
<div class='name'>".$row['name']."</div>
<div class='price'>$".$row['price']."</div>
<button type='submit' class='buy'>Buy Now</button>
</form>
</div>";
}
mysqli_close($con);
?>
<div style="clear:both;"></div>
<div class="message_box" style="margin:10px 0px;">
<?php echo $status; ?>
</div>