Welcome Guest, Not a member yet? Register   Sign In
Creating basic Register (username/login/password)
#1

[eluser]terry101[/eluser]
Hey i am working on building a register form but having trouble inserting into my database.
I am getting a error on line 37 on my controller (placed a star on line 37 in here). Anyone know where i went wrong?

error i'm getting
! ) Fatal error: Call to a member function register_user() on a non-object in C:\wamp\www\code\application\controllers\user.php on line 37

View

<html>
<head>
<title> Regristration form</title>

</head>
<body>
<h1>User Registration</h1>

<p>Please fill in the details below</p>

&lt;?php
echo form_open($base_url . 'user/register');

$username = array(
'name' => 'username',
'id' => 'username',
'value' => set_value('username')
);

$name = array(
'name' => 'name',
'id' => 'name',
'value' => set_value('name')
);

$email = array(
'name' => 'email',
'id' => 'email',
'value' => set_value('email')
);

$password = array(
'name' => 'password',
'id' => 'password',
'value' => ''
);

$password_conf = array(
'name' => 'password_conf',
'id' => 'password_conf',
'value' => ''
);
?&gt;
<ul>
<li>
<lable> Username</lable>
<div>
&lt;?php echo form_input($username);?&gt;
</div>
</li>

<li>
<lable> Name</lable>
<div>
&lt;?php echo form_input($name);?&gt;
</div>
</li>

<li>
<lable>E-mail Address</lable>
<div>
&lt;?php echo form_input($email);?&gt;
</div>
</li>

<li>
<lable> Password</lable>
<div>
&lt;?php echo form_password($password);?&gt;
</div>
</li>

<li>
<lable> Retype Password</lable>
<div>
&lt;?php echo form_password($password_conf);?&gt;
</div>
</li>

<li>
&lt;?php echo validation_errors();?&gt;
</li>

<li>
<div>
&lt;?php echo form_submit(array('name' => 'register'), 'Register');?&gt;
</div>
</li>
</ul>

&lt;?php echo form_close();?&gt;
&lt;/body&gt;
&lt;/html&gt;







Controller
[/b]&lt;?php
class User extends CI_Controller{

function __User() {
parent::__Controller();

$this->view_data['base_url'] = base_url();

$this->load->model('User_model');
}

function index() {

$this->register();
}

function register(){

$this->load->library('form_validation');

$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|xss_clean');
$this->form_validation->set_rules('name', 'Name', 'required|min_length[3]|max_length[20]|xss_clean');
$this->form_validation->set_rules('email', 'E-mail', 'required|min_length[3]|max_length[20]|xss_clean|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[3]|max_length[20]|xss_clean');
$this->form_validation->set_rules('password_conf', 'Re-type Password', 'required|min_length[3]|max_length[20]|xss_clean|matches[password]');

if ($this->form_validation->run() == FALSE) {

$this->load->view('viewregister');
}
else {
$username = $this->input->post('username');
$name = $this->input->post ('name');
$email = $this->input->post ('email');
$password = $this->input->post ('password');

********Line 37*********** $this->Usermodel->register_user($username, $name, $email, $password);


}
}
}






Model
&lt;?php

class Usermodel extends CI_Model {

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

function register_user($username, $name, $email, $password) {
$shal_password = shal($password);

$query_str = "INSERT INTO tbregister (username, password, name, email) Value ('{$username}', '{$name}', '{$email}', '{password}')";

$this->db-query($query_sr, array($username,$name,$email,$shal_password));
}
}
#2

[eluser]isawhat[/eluser]
"Usermodel" needs to be all lower case letters.
#3

[eluser]fornyhucker[/eluser]
Quote:$this->load->model(‘User_model’);
and
Quote:$this->Usermodel->register_user($username, $name, $email, $password);
see the difference? you forgot the "_" when call your model.
#4

[eluser]terry101[/eluser]
i dont get it i had it in quotes

Controller

$this->load->model(‘User_model’);
}

function index() {
#5

[eluser]terry101[/eluser]
wait i see what you mean i fixed it to Usermodel cuz i didnt have any "_" but i still get the same error. (my model was called Usermodel)

This is the error i get

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\wamp\www\register\application\controllers\user.php on line 37
#6

[eluser]toymachiner62[/eluser]
It's probably because you're loading model Usermodel, but then you're calling a function from the model User_model.
#7

[eluser]terry101[/eluser]
hmm isnt this going to the model Usermodel then going to the function register_user?

$this->Usermodel->register_user($username, $name, $email, $password);

What do you mean and where?
#8

[eluser]toymachiner62[/eluser]
[quote author="terry101" date="1312593327"]hmm isnt this going to the model Usermodel then going to the function register_user?

$this->Usermodel->register_user($username, $name, $email, $password);

What do you mean and where?[/quote]

Yes that would go to the model Usermodel and the function register_user, but in your constructor function, you've loaded the model 'User_model' instead of 'Usermodel'.
#9

[eluser]jvicab[/eluser]
make sure the name of your model is the same of your class. In your case should be something like usermodel.php.

The name of the model should be the same of the name of your new class extending CI_Model and the same what you put in $this->load->model('') and whe you call it.

model name: usermodel.php (model's filename doesn't need to be case sensitive, but the others do)
class declaration inside it: class Usermodel extends CI_Model
model load: $this->load->model('Usermodel');
ussage: $this->Usermodel->register_user($username, $name, $email, $password);
#10

[eluser]terry101[/eluser]
i changed it now to Usermodel without the "_"

View
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt; Regristration form&lt;/title&gt;

&lt;/head&gt;
&lt;body&gt;
<h1>User  Registration</h1>

<p>Please fill in the details below</p>

&lt;?php
echo form_open($base_url . 'user/register');
    
    $username = array(
        'name' => 'username',
        'id' => 'username',
        'value' => set_value('username')
    );
    
    $name = array(
        'name' => 'name',
        'id' => 'name',
        'value' => set_value('name')
    );
    
    $email = array(
        'name' => 'email',
        'id' => 'email',
        'value' => set_value('email')
    );
    
    $password = array(
        'name' => 'password',
        'id' => 'password',
        'value' => ''
    );
    
    $password_conf = array(
        'name' => 'password_conf',
        'id' => 'password_conf',
        'value' => ''
    );
?&gt;        
<ul>
    <li>
    <lable> Username</lable>
    <div>
        &lt;?php echo form_input($username);?&gt;
    </div>
    </li>
    
    <li>
    <lable> Name</lable>
    <div>
        &lt;?php echo form_input($name);?&gt;
    </div>
    </li>
    
    <li>
    <lable>E-mail Address</lable>
    <div>
        &lt;?php echo form_input($email);?&gt;
    </div>
    </li>
    
    <li>
    <lable> Password</lable>
    <div>
        &lt;?php echo form_password($password);?&gt;
    </div>
    </li>
    
    <li>
    <lable> Retype Password</lable>
    <div>
        &lt;?php echo form_password($password_conf);?&gt;
    </div>
    </li>
    
    <li>
            &lt;?php echo validation_errors();?&gt;    
    </li>
    
    <li>
    <div>
        &lt;?php echo form_submit(array('name' => 'register'), 'Register');?&gt;
    </div>
    </li>
</ul>    
        
&lt;?php echo form_close();?&gt;
&lt;/body&gt;
&lt;/html&gt;


Controller

Code:
&lt;?php
class User extends CI_Controller{

    function __User() {
        parent::__Controller();
          
        $this->view_data['base_url'] = base_url();
        
        $this->load->model('Usermodel');
        }
        
        function index() {
        
        $this->register();
        }
        
        function register(){
        
            $this->load->library('form_validation');
            
            $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|xss_clean');
            $this->form_validation->set_rules('name', 'Name', 'required|min_length[3]|max_length[20]|xss_clean');
            $this->form_validation->set_rules('email', 'E-mail', 'required|min_length[3]|max_length[20]|xss_clean|valid_email');
            $this->form_validation->set_rules('password', 'Password', 'required|min_length[3]|max_length[20]|xss_clean');
            $this->form_validation->set_rules('password_conf', 'Re-type Password', 'required|min_length[3]|max_length[20]|xss_clean|matches[password]');
            
            if ($this->form_validation->run() == FALSE) {
            
            $this->load->view('viewregister');
            }
            else {
                $username = $this->input->post('username');
                $name = $this->input->post ('name');
                $email = $this->input->post ('email');
                $password = $this->input->post ('password');
                
                $this->Usermodel->register_user($username, $name, $email, $password);
                
                
            }
        }
}


Model

Code:
&lt;?php

class Usermodel extends CI_Model {
    
        function __construct() {
             parent::__Model();
        }
        
        function register_user($username, $name, $email, $password) {
            $shal_password = shal($password);
            
            $query_str = "INSERT INTO tbregister (username, password, name, email) Value ('{$username}', '{$name}', '{$email}', '{password}')";
            
            $this->db-query($query_sr, array($username,$name,$email,$shal_password));
        }
    }


and i still get a error

( ! ) Fatal error: Call to a member function register_user() on a non-object in C:\wamp\www\code\application\controllers\user.php on line 37

below is line 37 from the controller
Code:
$this->Usermodel->register_user($username, $name, $email, $password);



any idea? i been cracking my head at this for so long and i have no clue :-(

my model name is usermodel.php (all lowercase)




Theme © iAndrew 2016 - Forum software by © MyBB