什么是我的代码,总是返回产品已经存在感到高兴?

i want to make some validations if the stock is greater than min_stock the do some stuff , else if max stock is less greater than stock then other stuff , but if everything is ok , but is not empty then msg successfully added and if not duplicate I have this , but I dont know why always has product already exists and if I change not add to my database..

php

public function addProduct(){
    $descripcion = $this->input->post('description');
    $cost_price =  $this->input->post('cost_price');
    $selling_price = $this->input->post('selling_price');
    $wprice = $this->input->post('wprice');
    $min_stock = $this->input->post('min_stock');
    $stock = $this->input->post('stock');
    $max_stock = $this->input->post('max_stock');
    $data = array(
        'descripcion' => $descripcion,
        'precio_compra' => $cost_price,
        'precio_venta' => $selling_price,
        'precio_mayoreo' => $wprice,
        'existencia_minima' => $min_stock,
        'existencia' => $stock,
        'existencia_maxima' => $max_stock
    );

    $product = $this->products->addProduct($data);
    if (!empty($product)) {
        $this->json(array('msg' => 'successfully added'));
    }else{
        $this->json(array('duplicate' => 'product already exists'));
    }

    if ($stock > $min_stock) {
        $this->json(array('min_stock' => 'el stock no puede ser mayor al min'));
    }elseif ($max_stock < $stock) {
        $this->json(array('min_stock' => 'el stock no puede ser mayor al max'));
    }else{

    }
}

validation

 if ($stock > $min_stock) {
       $this->json(array('min_stock' => 'el stock no puede ser mayor al min'));
   }elseif ($max_stock < $stock) {
        $this->json(array('min_stock' => 'el stock no puede ser mayor al max'));
  }else{
       /// add the product with the msgs ,but how can I paste  this code below to work property 
    if (!empty($product)) {
            $this->json(array('msg' => 'successfully added'));
        }else{
            $this->json(array('duplicate' => 'product already exists'));
        }

   }

firstly we need to know what is your product id.

then we need to get all products from product table and compare their with the currently product(compare products id), after that you can do an if condition, if the currently product exist in the products you don't need to insert it , then insert it.

i can help you with this code but u need to adapt him with ur code.

 public function addProduct(){

create new stdClass object

$product =  new stdClass();
$product->descripcion = $this->input->post('description');
$product->cost_price =  $this->input->post('cost_price');
$product->selling_price = $this->input->post('selling_price');
$product->wprice = $this->input->post('wprice');
$product->min_stock = $this->input->post('min_stock');
$product->stock = $this->input->post('stock');
$product->max_stock = $this->input->post('max_stock');`

get all products

$products = $this->products->getAllProducts();

compare all produts exist in ur database with ur new products

$test = false;

foreach($produts as $p){

 if($p->id == $product->id){
   $test = true;
 } }

if the product not exist , insert it

 if($test){
   $rslt =  $this->products->addProduct($product);
   }


if (!empty$rslt )) {
    $this->json(array('msg' => 'successfully added'));
}else{
    $this->json(array('duplicate' => 'product already exists'));
}

if ($stock > $min_stock) {
    $this->json(array('min_stock' => 'el stock no puede ser mayor al min'));
}elseif ($max_stock < $stock) {
    $this->json(array('min_stock' => 'el stock no puede ser mayor al max'));
}else{

}