Welcome Guest, Not a member yet? Register   Sign In
Simple database query
#1

[eluser]s0mmer[/eluser]
First of all hi to all here at CI forum.

My name is Steffen and i finally forced me to dive into a framework today. My choice was CI.

After working a bit today i start to get a nice feeling about it.

With that said, i have my first question ready, in hope some can help me:

Im trying to insert into a database..

A controller with some test code:
#Testing
$this->load->model('register', '', TRUE);
$this->Register->create_user();

Register model:
class Register extends Model {

function Register()
{
parent::Model();
}

function create_user()
{
$this->createdate = "2009-06-09";
$this->username = "test";
$this->password = "test";
$this->email = "test";
$this->name = "test";

$this->db->insert('pmd_users', $this);
}

}

Im also autoloading 'database' library and i have filled in my db informations.

But i am just getting a blank screen.. is that a sign of a mysql error or am i doing anything wrong with CI?

Best regards..
#2

[eluser]nicholas.byfleet[/eluser]
Ok, a couple of things:
1. Welcome to CI forums... we are glad to have you.
2. What are you trying to do with the extra parameters here:
Code:
$this->load->model(‘register’, ‘’, TRUE);
I usually just put
Code:
$this->load->model('Register');

3. I'm not really a PHP expert, but wouldn't you have to declare your variables like:
Code:
var $createdate;
var $username;
... etc etc.

before function Register() {... ?
3. Since you are not loading any views, it is entirely possible that your script is working just fine. Of course you can tell easily by looking at your database. did it create a new row when you ran it?

4. I usually have my functions return a boolean like this:

Code:
function create_user()
  {
      $this->createdate   = “2009-06-09”;
      $this->username     = “test”;
      $this->password     = “test”;
      $this->email     = “test”;
      $this->name         = “test”;

      if ($this->db->insert(‘pmd_users’, $this)) {
         return true;
      } else {
         return false;
      }
  }
that way, you can use the return value in your controller to load an error view if the procedure is unsuccessful.


In any case, like I said, I am no expert. Hope this is of some help to you.
#3

[eluser]s0mmer[/eluser]
[quote author="nicholas.byfleet" date="1253233510"]Ok, a couple of things:
1. Welcome to CI forums... we are glad to have you.
2. What are you trying to do with the extra parameters here:
Code:
$this->load->model(‘register’, ‘’, TRUE);
I usually just put
Code:
$this->load->model('Register');

3. I'm not really a PHP expert, but wouldn't you have to declare your variables like:
Code:
var $createdate;
var $username;
... etc etc.

before function Register() {... ?
3. Since you are not loading any views, it is entirely possible that your script is working just fine. Of course you can tell easily by looking at your database. did it create a new row when you ran it?

4. I usually have my functions return a boolean like this:

Code:
function create_user()
  {
      $this->createdate   = “2009-06-09”;
      $this->username     = “test”;
      $this->password     = “test”;
      $this->email     = “test”;
      $this->name         = “test”;

      if ($this->db->insert(‘pmd_users’, $this)) {
         return true;
      } else {
         return false;
      }
  }
that way, you can use the return value in your controller to load an error view if the procedure is unsuccessful.


In any case, like I said, I am no expert. Hope this is of some help to you.[/quote]

First of all thanks for your reply Smile

I've now tried deleting the ekstra parameters (i thought i should after reading the guide) and also declaring the variables im using. Still just blank screen. I am displaying a view, i just excluded it from this post..
#4

[eluser]nicholas.byfleet[/eluser]
hmmm.... if you don't mind, could you post all of your code?
#5

[eluser]s0mmer[/eluser]
My controller:
Code:
function index()
    {
        $data['title'] = "photomedoing";
        
        # Create form elements for sign up
        $data['frName']        = array('name' => 'frName', 'id' => 'frName');
        $data['frUsername'] = array('name' => 'frUsername', 'id' => 'frUsername');
        $data['frPassword'] = array('name' => 'frPassword', 'id' => 'frPassword');
        $data['frEmail']     = array('name' => 'frEmail', 'id' => 'frEmail');
        
        #Testing
        $this->load->model('register');
        $this->Register->create_user();
        
        # Display view
        $this->load->view('register', $data);
    }

My model:
Code:
<?php

class Register extends Model {
    
    var $createdate    = '';
    var $username    = '';
    var $password    = '';
    var $email        = '';
    var $name        = '';

    function Register()
    {
        parent::Model();
    }
    
    function create_user()
    {
        #$this->createdate    = date();
       /* $this->username        = $this->input->post('frUsername');
        $this->password        = $this->input->post('frPassword');
        $this->email        = $this->input->post('frEmail');
        $this->name            = $this->input->post('frName');
        */
        $this->createdate    = "2009-06-09";
        $this->username        = "test";
        $this->password        = "test";
        $this->email        = "test";
        $this->name            = "test";

        $this->db->insert('pmd_users', $this);
    }
    
    function check_username()
    {
        #todo
    }

}

?>
#6

[eluser]nicholas.byfleet[/eluser]
Oh okay, I see the problem. $this->db->insert() takes an array as the second parameter, you are trying to put an object instead.

Instead, try this sort of format:

Code:
$data = array('createdate' => "2009-06-09",
              'username' => "test",
              'password' => "test",
              'email' => "test",
              'name' => "test");

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

Let me know if you are having more trouble.
#7

[eluser]s0mmer[/eluser]
I've updated what you said.

But im still getting blank screen Sad As soon i load my model i get the blank screen.
#8

[eluser]jedd[/eluser]
[quote author="s0mmer" date="1253242719"]
But im still getting blank screen Sad As soon i load my model i get the blank screen.
[/quote]

Can you put echos in your controller, especially useful is to var_dump() variables.

You cite 'register' as a view, but we haven't seen that snippet of code yet. Since that's the thing that actually pushes stuff to the screen, it's probable that your problem is in there.
#9

[eluser]BrianDHall[/eluser]
Ok, so first try this:
Code:
function index()
    {
        $data['title'] = "photomedoing";
        
        # Create form elements for sign up
        $data['frName']        = array('name' => 'frName', 'id' => 'frName');
        $data['frUsername'] = array('name' => 'frUsername', 'id' => 'frUsername');
        $data['frPassword'] = array('name' => 'frPassword', 'id' => 'frPassword');
        $data['frEmail']     = array('name' => 'frEmail', 'id' => 'frEmail');
        
        #Testing
        //$this->load->model('register');
        //$this->Register->create_user();
        
        # Display view
        $this->load->view('register', $data);
    }

This produces output right, so it's only when the model loads that you have a problem? If so, try uncommenting the load model line only - does it still work? If so then its the register line.

I don't know that it matters, but technically the line should be:

Code:
$this->register->create_user();

Lower-case being the only change.

Once you've narrowed it down this far it's easier to see if something is wrong with your model file or just the function itself.
#10

[eluser]jedd[/eluser]
[quote author="BrianDHall" date="1253249800"]
I don't know that it matters, but technically the line should be:

Code:
$this->register->create_user();
[/quote]

In fact, the lines should be
Code:
$this->load->model('Register');
$this->Register->create_user();

As per the [url="http://ellislab.com/codeigniter/user-guide/general/models.html#loading"]CI User Guide - Loading Models[/url] section.




Theme © iAndrew 2016 - Forum software by © MyBB