Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to get the error when saving a record?

hi i am having a problem about catching the specific error when saving a record on my database

$newproduct = new Tblproducts();

$newproduct->prod_id            = $prodid;           
$newproduct->prod_itemcode      = $prodcode;         
$newproduct->prod_brand         = $prodbrand ;       
$newproduct->prod_definition    = $proddifinition;   
$newproduct->prod_category      = $prodcat;          
$newproduct->prod_subcat        = $prodsubcat;       
$newproduct->prod_supplier      = $prodsupp;        
$newproduct->prod_manufacturer  = $prodmanu;         
$newproduct->prod_unit          = $produnit;         
$newproduct->prod_bigunit       = $prodbigunit;      
$newproduct->prod_smallunit     = $prodsmallunit;    
$newproduct->prod_physicalcount = $prodphysicalcount;
$newproduct->prod_batchnumber   = $prodbatchnum;     
$newproduct->prod_discount      = $proddisc;         
$newproduct->prod_istaxable     = $prodvat;          
$newproduct->prod_sellprice     = $prodcost;         
$newproduct->prod_purchaseprice = $prodpurchasecost; 
$newproduct->prod_profit        = $prodprofitvalue;  
$newproduct->prod_purchasedate  = $prodpurchasedate; 
$newproduct->prod_expiredate    = $prodexpiredate;   
$newproduct->prod_status        = $prodstatus;       
$newproduct->prod_remarks       = $prodremarks;      
$newproduct->prod_addedby       = $addedby;          
$newproduct->prod_lastupdate    = $dateadded; 

if(!$newproduct->save()){
echo"error";
}
else{
echo"record saved";
}

what i want to do is get the specific error on why does the error occurs any help will do



4.5k
Accepted
answer
edited Aug '14

As from guide

if ($newproduct->save() == false) {
  echo "Umh, We can't store product: ";
  foreach ($newproduct->getMessages() as $message) {
    echo $message;
  }
 } else {
  echo "Great, a new product was saved successfully!";
 }

@Nicola wow that really works, huge thanks to you man.