Welcome Guest, Not a member yet? Register   Sign In
Undefined variable: error_message?
#1

[eluser]anna16[/eluser]
hi guys i have this error message below,
Code:
A PHP Error was encountered

Severity: Notice
Message: Undefined variable: error_message
Filename: membership/register_form.php
Line Number: 27


By the way here is the codes for controller site.php
Code:
<?php
class Site extends Controller
{

    //function __construct()
  function Site()
    {
        parent::Controller();
        $this->load->library('session');
        $this->load->helper('form');
    $this->load->helper('url');
    $this->load->model('membership/membership');
    }
  
  function register_form()
  {
       $this->load->view('membership/register_form');
  }


    function register_account()
    {
        $data = array(
            'email' => $this->input->post('email'),
            'username' => $this->input->post('username'),
      'password' => $this->input->post('password'),
      'confirm' => 'no',
        );

    //check email
    $data['error_message'] = $this->Membership->check_email();
    $this->load->view('register_form', $data);    
    
    
    //check username
            
        //$this->membership->add_record($data);
    //$this->load->view('membership/register_success');
    }


/* These are function tests. */  
  function register_success()
  {
       $this->load->view('membership/register_success');
  }
  

}
//end of class


and here is the codes for model membership.php
Code:
<?php
class Membership extends Model
{

  function __construct()
  {
    parent::Model();
    $this->load->database();
  }

    function add_record($data)
    {
        $this->db->insert('user', $data);
        return;
    }
  
  function check_email()
  {
    $query = $this->db->query("SELECT email FROM membership WHERE email='$this->input->post('email')");
    
    //check email
    if ($query->num_rows() > 0) //if exist assign message as data
    {
            return $error_message = "Sorry the email is already in used, please try again.";
    }
    else  //else proceed to check_username()
    {
           return $error_message = "Nice, no one used this email yet";
    }
  }
  

  function check_username()
  {
    $query = $this->db->query("SELECT username FROM membership WHERE username='$this->input->post('username')");
    if ($query->num_rows() > 0)
    {
    
    }    
  }



/*    
    function get_records()
    {
        $query = $this->db->get('data2');
        return $query->result();
    }
    
    function update_record($data2)
    {
        $this->db->where('id', 12);
        $this->db->update('data2', $data2);
    }
    
    function delete_row()
    {
        $this->db->where('id', $this->uri->segment(3));
        $this->db->delete('data2');
    }
*/    
}
//end of membership_model



Thanks in advanced.
#2

[eluser]Twisted1919[/eluser]
You have the method :
Code:
function register_form()
  {
       $this->load->view('membership/register_form');
  }
but you don't pass any data hence the error you receive, because you call a variable that is not defined/passed from your controller.
#3

[eluser]jedd[/eluser]
As an aside, this looks cumbersome:

[quote author="anna16" date="1290269515"]
Code:
if ($query->num_rows() > 0) //if exist assign message as data
{
   return $error_message = "Sorry the email is already in used, please try again.";
}
else  //else proceed to check_username()
{
   return $error_message = "Nice, no one used this email yet";
}
[/quote]

Generally speaking you should have one return point - or at least strive towards that - in any function. Alternative assessment - assigning a value to a variable that is instantly discarded is wasteful.

I'd suggest:
Code:
if ($query->num_rows() > 0) //if exist assign message as data
{
   $error_message = "Sorry the email is already in used, please try again.";
}
else  //else proceed to check_username()
{
   $error_message = "Nice, no one used this email yet";
}
return $error_message;

Alternatively, just return (in each place) the string directly - eg. return "Sorry the email is already in used, please try again.";

For maximum brevity:
Code:
return ($query->num_rows() > 0)  ?  "Sorry the email is already in used, please try again."  : "Nice, no one used this email yet" ;

Finally, when posting errors can you please identify the line the error occurs in - line 27 of the fragment you quoted doesn't appear to be able to generate the error you're seeing.




Theme © iAndrew 2016 - Forum software by © MyBB