<?php 
require_once 'config.php';
$error=false;
$errors=array();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   
  
   $user_id=$_POST['user_id'];
   $product_id=$_POST['product_id'];
   $quantity=$_POST['quantity'];
	$rowcount=0;
		 $sql12="SELECT id FROM cart_items where user_id=".$user_id." AND product_id =".$product_id;
		 if ($result12=mysqli_query($con,$sql12)){			 
			  $rowcount12=mysqli_num_rows($result12);
				if($rowcount12>0){
					$createitem=mysqli_query($con,"UPDATE cart_items SET quantity ='".$quantity."' WHERE user_id = '".$user_id."' AND product_id ='".$product_id."'");
				}else{
					$createitem=mysqli_query($con,"INSERT INTO cart_items (user_id,product_id,quantity,status) VALUES('".$user_id."','".$product_id."','".$quantity."',1)");
				}
		}
		
	 if($createitem){
		 $sql="SELECT id FROM cart_items where user_id=".$user_id;

			if ($result=mysqli_query($con,$sql)){			 
			  $rowcount=mysqli_num_rows($result);			  
			  }
	     	$errors['cartitems']=$rowcount;
	     	$errors['success']=true;
	     	$errors['messages']='Item added to cart Sucessfully';
	 }else{
	     $errors['success']=false;
	     $errors['message']='Somthing went wrong !!';
	 }
 	 
	 	echo json_encode($errors);
}