简体   繁体   中英

undefined variable error in codeigniter model

I am working on a registration form When i submit the form after the entering details the registration data get stored in the database but the the page shows the following error

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: dbRet

Filename: models/registration_model.php

Line Number: 47

I am not getting the prolem the same code is working good at the localhost but what is the problem at server??

This is the model Code

class Registration_model extends CI_Model {

function __construct()
{
    parent::__construct();
}

function index()
{
    $data['page_title'] = 'Registration';
    $this->load->view('client/general/head',$data);
    $this->load->view('client/general/header');
    $this->load->view('registration');
    $this->load->view('client/general/footer');
}
function busregister()
{
    $data['page_title'] = 'Registration';
    $this->load->view('business/registration1');

}

public function save_registration($un,$email,$pwd,$utype)
{
try
{
  $data = array(
 'username' => $un,
 'password' => $pwd,
 'email' => $email
 );
//print_r($data);
//die();
$this->load->database();
$this->db->reconnect();
if($utype=='buss')
{
    $dbRet=$this->db->insert('business', $data);     
}
else
{
    $dbRet-$this->db->insert('user', $data); 
}   
if (!$dbRet) {
  $ret['ErrorMessage'] = $this->db->_error_message();
  $ret['ErrorNumber'] = $this->db->_error_number();
  log_message('error', "DB Error: (".$ret['ErrorNumber'].") ".$ret['ErrorMessage']);
  $this->db->close();
return $ret['ErrorNumber'];
  }
  else{
   $rid=$this->db->insert_id();
$this->db->close();
return $rid;
  }
}
catch (Exception $e){
return -1;
}
}
}?>

This is the controller code

class Registration extends CI_Controller {
public function index()
{
$this->load->helper(array('registration','date','country'));
$this->load->helper('client');
$this->load->model('Registration_model');
if ($this->input->post("submit_registration",TRUE))
{
 if($this->form_validation->run("stud_registration_rules"))
 {
    $this->load->library(array('encrypt'));
    $stud_first_name = $this->input->post('stud_user_name');
    $stud_email = $this->input->post('stud_email');
    $stud_pass = $this->input->post('stud_password');
    $retval=$this->Registration_model->save_registration($stud_first_name,$stud_email,$this->encrypt->encode($stud_pass),"user");
    var_dump($this->encrypt->encode($stud_pass));
    if($retval!==-1){   //SAVE SUCCESS
            $this->session->set_flashdata('flashSuccess', 'Record saved successfully.');
            redirect('/');
            }
            else{
            $this->session->set_flashdata('flashError', 'Error Saving Record');
            redirect('/registration');
            }
        }
        else{
        // FORM IS NOT VALIDATED
        $this->session->set_flashdata('flashError', 'Validation fields error');
            redirect('/registration');
        }
}

    $this->Registration_model->index();

} }

This is the view code

<div class="content">
        <div class="container_12">
        <div class="grid_12">
        <?php if($this->session->flashdata('flashSuccess')) : ?>
        <div class="alert alert-success">
        <a href="#" class="close" data-dismiss="alert">&times;</a>
        <p><?php print_r($this->session->flashdata('flashSuccess'));
        ?></p>
        </div>
        <?php endif; ?>        
        <?php if($this->session->flashdata('flashError')) : ?>
        <div class="alert alert-danger">
        <a href="#" class="close" data-dismiss="alert">&times;</a>
        <p><?php print_r($this->session->flashdata('flashError'));
        ?></p>
        </div>
        <?php endif; ?>
            <?php echo registration_form();?>
            </div>
        </div>

In your Model code Registration_model , Else part assignment operator is wrong

change the following line

$dbRet-$this->db->insert('user', $data);

to

$dbRet = $this->db->insert('user', $data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM