Welcome Guest, Not a member yet? Register   Sign In
Beginner, needs some help
#1

[eluser]kjanko[/eluser]
Using CodeIgniter framework for the first time, and experiencing the first issues with it. This is an account registration module, however the only data that's insert inside the database is 0 in all columns.
Full code won't be shown.

view:
Code:
<form action="http://delisium.com/register/make" method="post">
                    
                    <div class="row">
                        <div class="half-left">
                            <label>Username:</label>
                            &lt;input type="text" value="Username" /&gt;
                        </div>
                        
                        <div class="half">
                            <label>Password:</label>
                            &lt;input type="password" value="Password" /&gt;
                        </div>
                        
                        <div class="half-left">
                            <label><br />E-Mail:</label>
                            &lt;input type="text" value="Email" /&gt;
                        </div>
                        
                        <div class="half">
                            <label><br />Age:</label>
                            &lt;input type="text" value="Age" /&gt;
                        </div>
                    </div>
                    
                    <div class="row logged">                    
                        <div class="buttons">
                            <button type="submit"><span>Create</span></button>
                        </div>
                    </div>
                    
                &lt;/form&gt;

controller:
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Register extends CI_Controller
{
    public function index()
    {
        if(!$this->session->userdata('loggedin'))
        {
            $this->load->view('register');
        }
        else
        {
            redirect('homepage');
        }
    }
    
    public function make()
    {
        $this->load->model('register_model', 'user');
        $username = $this->input->post('Username');
        $password = $this->input->post('Password');
        $sha_pass = sha1(strtoupper($password));
        $email    = $this->input->post('Email');
        $age      = $this->input->post('Age');
        $this->user->MakeAccount($username, $sha_pass, $email, $age);
        redirect('login');
    }
}

model:
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Register_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }
    
    function MakeAccount($username, $password, $email, $age)
    {
        if($this->session->userdata('loggedin'))
        {
            echo 'You\'re already logged in.';
            rederict('homepage');
        }
        else
        {
            if(!$this->CheckUsername($username))
            {
                echo 'An account with that username already exists.';
            }
            elseif(!$this->CheckMail($email))
            {
                echo 'An account with that email already exists.';
            }
            else
            {
                $data = array(
                'username' => $username,
                'password' => $password,
                'email'    => $email,
                'age'      => $age
                );
                $this->db->insert('users', $data);
            }
        }    
    }
    
    function CheckUsername($username)
    {
        $query = $this->db->select('username')->from('users')->where('username', $username)->get();
        if($query->num_rows() <= 0)
            return true;
        else
            return false;
    }
    
    function CheckMail($email)
    {
        $query = $this->db->select('email')->from('users')->where('email', $email)->get();
        if($query->num_rows() <= 0)
            return true;
        else
            return false;
    }
}

Any help appriciated.
#2

[eluser]OliverHR[/eluser]
Hello Kjanco,

Code:
if($this->session->userdata('loggedin'))
{
    echo 'You\'re already logged in.'; [1]
    rederict('homepage'); [2]
}

1 - That throws you an error because you cannot send output before using redirect check in docs:

http://ellislab.com/codeigniter/user-gui...elper.html

2 - Is redirect not rederict

And for data debug use:

Code:
log_message('debug', print_r($data, true)); // For the array in your insert.

Hope this help you.

Full solution won’t be posted. Smile
#3

[eluser]kjanko[/eluser]
if($this->session->userdata('loggedin')) is always false since I don't have an account that I can use for login Smile however I corrected those mistakes.

Next,
$data = array(
'user' => $username,
'pass' => $sha_pass,
'mail' => $email,
'agee' => $age
);
$this->user->MakeAccount($data['user'], $data['pass'], $data['mail'], $data['agee']);
log_message('debug', print_r($data, true));

This is what I did to the make method. However nothing is printed when I access it.
#4

[eluser]osci[/eluser]
try
Code:
if($query->num_rows() > 0)
   return FALSE
else
   return TRUE;
#5

[eluser]kjanko[/eluser]
Osci, isn't it the same ? -.-'
#6

[eluser]osci[/eluser]
I thought it might be a cast problem.
I'm not sure about the <
maybe you could use == 0 instead of <= 0
#7

[eluser]kjanko[/eluser]
I think there's a problem with retrieving the post data, which might be the reason why nothing is inserted in the db.
#8

[eluser]kjanko[/eluser]
I feel like bumping this as I can't solve it.

EDIT: So simple, yet annoying. I forgot to name my input fields Smile
/request closed thread.
#9

[eluser]InsiteFX[/eluser]
Change this for one!
Code:
// from this:
if($this->session->userdata('loggedin'))

// to this:
if($this->session->userdata('loggedin') == TRUE)

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB