Get true/false codeigniter insert function - php

every time I use insert function in my model, the return always true, here's my code
public function insert_text($data)
{
return $this->db->table($this->table)->insert($data);
}
call code
if ($this->text->insert_text($data)) {
echo "success";
} else {
echo "failed";
}
even the execution failed (cause of same primary key already exists), code always return "success", how to fix it?
Thanks

check if inserted successfuly
if ($this->db->insert_id()) {
echo "success";
} else {
echo "failed";
}

Related

error 500 with Call to a member function on bool

I am using Fat Free Framework but I got this error when trying to check non-existent data ID with dry() function
my code below will throw Call to a member function dry() on bool error if I supply it with non-existent ID value. It should echo the Empty Cursor echo if the ID not found instead if the error. If I use an ID that is within the database, it will show the id exist.
if(is_numeric($param['id']))
{
$id = $param['id'];
//check if data exist
$data = $data->load($id);
if(!$data->dry())
{
echo 'Data with id:'.$id.' exist';
}
else echo 'Cursor is empty. Data with id:'.$id.' doesnt exist!';
die();
}
else $f3->reroute('/');
how do I check if the ID exists without the error?
If your load() method returns false when the data isn't found, then you should be checking for this before you try any further operations on this value.
I've also changed the test to be
if($data !== false) {
so that if your method returns some other value which looks like false (0, null etc), then this will ensure it only picks up false...
if(is_numeric($param['id']))
{
$id = $param['id'];
//check if data exist
$data = $data->load($id);
if($data !== false) {
echo 'Data with id:'.$id.' exist';
}
else {
echo 'Cursor is empty. Data with id:'.$id.' doesnt exist!';
}
die();
}
else {
$f3->reroute('/');
}

Need to check if mysql 1062 error occured and return a value to controller

I need to check if a duplicate entry error occurred and if it occurred I need to return a value to my controller.
This is what i have done in my model
function ins($data)
{
$this->load->database();
if($this->db->insert('users',$data))
{
return "success";
}
else
{
return "fail";
}
}
It doesn't work. How can I make it work?
Thank you

Can`t get returned boolean value from class function call

I cant get returned value from php function call.
...
$dey=$validate->yoxla($this->ad,$this->soyad,$this->istadi,$this->sifre,$this->email);
if($dey){ // i cant get returned value from here. even it is true.
echo 'asd';
if(!$checkuser->checkusername($this->istadi)){
$err=array();
$err['code']='code14';
$err['aciglama']=$code14;
print json_encode($err);
die();
}elseif(!$checkuser->checkemail($this->email)){
$err=array();
$err['code']='code15';
$err['aciglama']=$code15;
print json_encode($err);
die();
}else{
$err=array();
$err['code']='acig';
print json_encode($err);
die();
}
}
yoxla() function:
...
elseif (!preg_match("/^.*(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $sifre)){
$err=array();
$err['code']='code11';
$err['aciglama']=$code11;
print json_encode($err);
die();
}elseif($email==""){
$err=array();
$err['code']='code12';
$err['aciglama']=$code12;
print json_encode($err);
die();
}elseif (!$this->validEmail($email)) {
$err=array();
$err['code']='code13';
$err['aciglama']=$code13;
print json_encode($err);
die();
}else{
return true;
}
I call first one by ajax request. and there is nothing returning to ajax or i cant get returned value from getresult() function.
Remove () from From class declaration :
try this :
class Form{
public function getresult(){
return true;
}
}
$validate=new Form();
$result=$validate->getresult();
if($result){
echo 'true';
}else{
echo 'false';
}
Enable php error logging so that you could understand such errors on browser : http://php.net/manual/en/function.error-reporting.php

Codeigniter: function return statement not returning

I am trying to return a db value using the following function in case of failure the function is supposed to return an error object. I have separated the code block with If/else statement. The if condition satisfies and the if block is executed. But for some reason the return statement in the if block do not execute and the code execution continues to next statement. I tried the two options on both cases the results are same.
First Code
public function is_defined_headtype($head_id)
{
if ($is_defined = $this->ci->account_head_model->is_defined_headtype($head_id))
{
echo "Im here<br>";
return $this->_success($is_defined, ACC_SUCCESS);
}
echo "Im here also";
// General Error Occurred
return $this->_general_error();
}
Second Code
public function is_defined_headtype($head_id)
{
if ($is_defined = $this->ci->account_head_model->is_defined_headtype($head_id))
{
echo "Im here<br>";
return $this->_success($is_defined, ACC_SUCCESS);
}
else
{
echo "Im here also";
// General Error Occurred
return $this->_general_error();
}
}
In both cases I am getting the following output.
Im here
Im here also{"err":1,"code":1,"msg":"Error"}
Calling function definition
public function add_eam($user_id, $eamname, $entityid, $headid, $eamdescription, $eamisfraction, $eampercentage, $eamamount, $affectedheadid)
{
if (/* $eam_id = $this->ci->account_eam_model->insert_eam($user_id, $eamname, $entityid, $headid) */ true)
{
$is_defined = $this->is_defined_headtype(/* $headid */1);
echo json_encode($is_defined);
if($is_defined == 1)
{
echo "Im here";
exit();
if ($entity_id = $this->ci->account_eam_definition_model->insert_eam_definition(/* $user_id */ 1, $eam_id, $eamdescription, $eamisfraction, $eampercentage, $eamamount, $affectedheadid))
{
return $this->_success($eam_id, ACC_ENTITY_ADDED);
}
else
{
return $this->_general_error();
}
}
echo "Im not here";
exit();
return $this->_success($eam_id, ACC_ENTITY_ADDED);
}
// General Error Occurred
return $this->_general_error();
}
hi i think the error is here
you are doing an if statement with only one '=' this is to set a value but you want to comapre so that should be:
'==' to compare that two values are the same
'!=' to see if the two values are different
public function is_defined_headtype($head_id)
{
if ($is_defined = $this->ci->account_head_model->is_defined_headtype($head_id))
{
echo "Im here<br>";
return $this->_success($is_defined, ACC_SUCCESS);
}
echo "Im here also";
// General Error Occurred
return $this->_general_error();
}
In If statement try Type Juggling (int) or use === for strong compare with both side data.

Php function include

So, I'm working on a dynamic forum signatures script and I alredy got it working. Now I will to make it so that only a specific user group can a specific design.
This is the function I made.
function userGroup($rank)
{
if ($rank == 38)
{
return true;
}
else
{
return false;
}
}
And used it like this.
if ($userGroup == true)
{
...
}
else echo('User with that ID doesn\'t exist in the database');
}
else echo('This user hasn\'t in the specific user group');
But it wont work like that.
Regards,
Lazar!
You made a function function userGroup($rank) and you are trying to call it as if its a variable $userGroup. What happened to the parameter? Your if-statement should be something like:
if (userGroup($var) == true) { ... }
You don't actually need the == true either:
if (userGroup($var)) { ... }
you can not use a function like this. you should have a function call with valid argument like this:
if (userGroup($intUserRank))
{
...
}
else echo('User with that ID doesn\'t exist in the database');
assume $intUserRank containing user rank.
You function could just be like this:
function userGroup($rank)
{
return ($rank == 38);
}
Then you would use it like this:
if (userGroup($some_rank))
{
...
}

Categories