php - Deleting Table Data Before Inserting New Data -
i have these 2 tables, table , table b. before updating table b want delete it's datas before inserting new one.
i have query delete data in table , insert new 1 after succeeded in deleting old records. inserting values it's not deleting old one, ended duplicating of new id's inserted before. tried code:
public function editdiscount(){ foreach ($this->searchfields $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() $key){ $devmessage[] = $key->getmessage(); } return $this->respond(array( 'usermessage' => 'failed', 'devmessage' => $devmessage, 'more' => 'failed update. 1 or more fields failed on validation.')); }else if ($discounttransaction == 'p'){ foreach ($this->searchfields $field => $value) : if($field == 'prodkey'){ $productid = $value; } endforeach; $exs = explode('|',$productid); $deleteerrors = 0; foreach ($exs $xx) { $deleteproductdiscount = vldiscountproduct::query() ->andwhere("discountid =?1") ->andwhere("app_id =?2") ->bind(array(1=>(int)$xx,2=>$this->session->get('appid'))) ->execute(); foreach($productdiscountdelete $cat){ $productsdelete = vldiscountproduct::findfirst($cat->$discountupdate->autokey); if ($deleteproductdiscount != false) { if ($productsdelete->delete() == false) { $deleteerrors++; $devmessage = array(); foreach ($deleteproductdiscount->getmessages() $key){ $devmessage[] = $key->getmessage(); } } } } unset($deleteproductdiscount); } if($deleteerror == 0){ $exp = explode('|',$productid); foreach ($exp $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() $key){ $devmessage[] = $key->getmessage(); } return $this->respond(array( 'usermessage' => 'failed', 'devmessage' => $devmessage, 'more' => 'failed create. 1 or more fields failed on validation.' )); } } return $this->respond(array('usermessage' => 'ok')); }else{ return $this->respond(array( 'usermessage' => 'failed', 'devmessage' => $devmessage, 'more' => 'failed delete. 1 or more fields produced error.' )); } return $this->respond(array( 'usermessage' => 'failed', 'devmessage' => 'cannot find user information', 'more' => 'failed update. 1 or more fields failed on validation.')); } } }
you're checking if($deleteerror == 0){
instead of if($deleteerrors == 0){
.
update
this whole foreach statement off.
foreach($productdiscountdelete $cat){ $productsdelete = vldiscountproduct::findfirst($cat->$discountupdate->autokey); if ($deleteproductdiscount != false) { if ($productsdelete->delete() == false) { $deleteerrors++; $devmessage = array(); foreach ($deleteproductdiscount->getmessages() $key){ $devmessage[] = $key->getmessage(); } } } }
you never define $productdiscountdelete
, it's never going foreach loop.
Comments
Post a Comment