Reputation: 1571
I want to show error for database in cakephp. How can I display a message of "sql error" to the users. This is my code. In this code, I am getting Database Error. But I want to show proper message to the user instead of database error.
public function edit_mothertongue(){
$motherTongue=array();
if($this->request->is('post')){
foreach ($this->request->data as $key => $value) {
$motherTongue[$key]=$value;
}
if(!empty($motherTongue)){
App::import('Model','MotherTongue');
$this->MotherTongue=new MotherTongue();
try{
if($this->MotherTongue->save($motherTongue)){
echo "Record saved";
}else{
echo "Record not saved";
}
}
catch(Exception $e){
echo $e->getMessage(); // I want to display error message of sql.
}
}
}
}
Upvotes: 0
Views: 1016
Reputation: 41
If you want to use AJAX you can set it like this:
$this->set('_serialize', array('errormsg'));
If you are looking for an output in JSON
However using AJAX is out of scope of this question.
Upvotes: 1