在插入新数据之前删除表数据

I have these two tables, table A and table B. Before updating my table B I want to delete it's datas before inserting a new one.

I have this query where it will delete the data in that table and insert a new one after it succeeded in deleting the old records. It is inserting the values but it's not deleting the old one, so it ended up duplicating some of my new id's that were already inserted before. I tried it with this code:

 public function editDiscount(){    
    foreach ($this->searchFields as $field => $value) :
        if($field == 'KEY'){
            $key = $value;
        } else if($field == 'NAME'){
            $discountName = $value;
        } else if($field == 'CODE'){ 
            $discountCode = $value;
        } else if($field == 'DESC'){
            $discountDesc = $value;
        }else if($field == 'TYPE'){
            $discountType = $value;
        }else if($field == 'TRANS'){
            $discountTransaction = $value;
        }else if($field == 'EXPTYPE'){
            $expiration = $value;
        }else if($field == 'START'){
            $expStartDate = $value;
        }else if($field == 'END'){
            $expEndDate = $value;
        }else if($field == 'VALUE'){
            $discountValue = $value;
        }else if($field == 'PRODKEY'){
            $productId= $value;
        } 
    endforeach;    

    $discount = VlDiscount::query()
            ->andWhere("autokey =?1")
            ->andWhere("app_id =?2")
            ->bind(array(1=>$key,2=>$this->session->get('appId')))
            ->execute();

    if($discount->count() != 0){

        $discountUpdate = VlDiscount::findFirst($key);
        $discountUpdate->discountName           = $discountName;
        $discountUpdate->discountCode           = $discountCode;
        $discountUpdate->discountDesc           = $discountDesc;
        $discountUpdate->discountType           = $discountType;
        $discountUpdate->discountTransaction    = $discountTransaction;
        $discountUpdate->expiration             = $expiration;
        $discountUpdate->expStartDate           = $expStartDate;
        $discountUpdate->expEndDate             = $expEndDate;
        $discountUpdate->discountValue          = $discountValue;



        if($discountUpdate->update() == false){
            $devMessage = array();
            foreach ($discountUpdate->getMessages() as $key){
                $devMessage[] =  $key->getMessage();
            }
            return $this->respond(array(
                    'userMessage' => 'Failed',
                    'devMessage' => $devMessage,
                    'more' => 'Failed to update. One or more fields failed on validation.'));
            }else if ($discountTransaction == 'p'){

                foreach ($this->searchFields as $field => $value) :
                if($field == 'PRODKEY'){
                    $productId = $value;
                }
                endforeach;

                $exs = explode('|',$productId);
                $deleteErrors = 0;
                foreach ($exs as $xx) {
                    $deleteproductDiscount = VlDiscountProduct::query()
                    ->andWhere("discountId =?1")
                    ->andWhere("app_id =?2")
                    ->bind(array(1=>(int)$xx,2=>$this->session->get('appId')))
                    ->execute();

                    foreach($productdiscountDelete as $cat){
                        $productsDelete = VlDiscountProduct::findFirst($cat->$discountUpdate->autokey);
                        if ($deleteproductDiscount != false) {
                            if ($productsDelete->delete() == false) {
                                $deleteErrors++;
                                $devMessage = array();
                                foreach ($deleteproductDiscount->getMessages() as $key){
                                    $devMessage[] =  $key->getMessage();
                                }
                            }
                        }
                    }
                    unset($deleteproductDiscount);
                }

                if($deleteError == 0){
                    $exp = explode('|',$productId);

                    foreach ($exp as $item){

                        $productDiscount = new VlDiscountProduct();
                        $productDiscount->discountId    = $discountUpdate->autokey;
                        $productDiscount->productId     = $item;
                        $productDiscount->app_id        = $this->session->get('appId');

                        if($productDiscount->create() == false){
                            $devMessage = array();
                            foreach ($productDiscount->getMessages() as $key){
                                $devMessage[] =  $key->getMessage();
                            }
                            return $this->respond(array(
                                    'userMessage' => 'Failed',
                                    'devMessage' => $devMessage,
                                    'more' => 'Failed to create. One or more fields failed on validation.'
                            ));
                        }

                    }
                    return $this->respond(array('userMessage' => 'OK'));
                }else{
                    return $this->respond(array(
                            'userMessage' => 'Failed',
                            'devMessage' => $devMessage,
                            'more' => 'Failed to delete. One or more fields produced an error.'
                    ));
                }
                return $this->respond(array(
                        'userMessage' => 'Failed',
                        'devMessage' => 'Cannot find user information',
                        'more' => 'Failed to update. One or more fields failed on validation.'));   
                }
            }
        }

You're checking if($deleteError == 0){ instead of if($deleteErrors == 0){.

UPDATE

This whole foreach statement is off.

foreach($productdiscountDelete as $cat){
    $productsDelete = VlDiscountProduct::findFirst($cat->$discountUpdate->autokey);
        if ($deleteproductDiscount != false) {
            if ($productsDelete->delete() == false) {
                $deleteErrors++;
                $devMessage = array();
                foreach ($deleteproductDiscount->getMessages() as $key){
                    $devMessage[] =  $key->getMessage();
                }
            }
        }
}

You never define $productdiscountDelete, so it's never going into this foreach loop.

i also change

 $productsDelete = VlDiscountProduct::findFirst($cat->$discountUpdate->autokey);

to

$productsDelete = VlDiscountProduct::findFirst($cat->autokey);