0

i've lost a good few hours on this and i'm sure it's something quite simple!

I'm quite new to codeigniter.

Basically i want to perform a simple INSERT into my database using values recieved from my form here is my view:

<!DOCTYPE html>
    <head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Page Title</title>

   </head>
   <body>

<div id="formage">

    <?php echo validation_errors(); ?>
    <?php echo form_open('../add_client'); ?>
    <p>
    <?php echo form_label('First Name', 'first_name');?>
    <?php echo form_input('first_name', '', 'id="first_name"' );?>
    </p>
    <p>
    <?php echo form_label('Last Name', 'second_name');?>
    <?php echo form_input('second_name', '', 'id="second_name"' );?>
    </p>
    <p>
    <?php echo form_label('Email Address', 'email_address');?>
    <?php echo form_input('email_address', '', 'id="email_address"' );?>
    </p>
    <p>
    <?php echo form_label('Password', 'password');?>
    <?php echo form_password('password', '', 'id="password"' );?>
    </p>
    <p>
    <?php echo form_label('Confirm Password', 'passwordconf');?>
    <?php echo form_password('passwordconf', '', 'id="passwordconf"' );?>
    </p>
    <p>
    <?php echo form_submit('submit', 'Add New Client');?>
    </p>


    <?php echo form_close(); ?>

</div>

This calls my controller:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed');

class Add_client extends CI_Controller{

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

function index(){

    $this->load->library('form_validation');
    $this->form_validation->set_rules('first_name', 'First Name', 'required');
    $this->form_validation->set_rules('second_name', 'Second Name', 'required');
    $this->form_validation->set_rules('email_address', 'Email Address', 'required|valid_email');
    $this->form_validation->set_rules('password', 'Password', 'required|min_length(4)');
    $this->form_validation->set_rules('passwordconf', 'Password', 'required|matches[password]');

    if($this->form_validation->run() !== false){
        $this->load->model('add_client_model');
        $this->add_client_model->addUser(
        $this->input->post('first_name'),
        $this->input->post('second_name'),
        $this->input->post('email_address'),
        $this->input->post('password'));

    }



    $this->load->view('add_client_view');

}


 }

Which in turn calls the addUser function from my model:

<?php

  class Add_client_model extends CI_Model{

function __construct(){
}

public function addUser($fname, $sname, $email, $password){

        $this->db->set('first_name', $fname);
        $this->db->set('second_name', $sname); 
        $this->db->set('email_address', $email); 
        $this->db->set('pasword', sha1($name)); 
        $this->db->insert('users');
}
 }

The problem i am having is that whenever i submit my form and the function is called it seems to insert the variable name like so (see second row):

https://dl.dropbox.com/u/76853467/Picture%204.png

I cant seem to get it to accept the post data i plug into my function parameters!

Any suggestions? Much thanks!

4
  • I know this is a way to do it but have you tried: $this->db->insert('users', array('first_name' => $fname,'second_name' => $sname,'email_address', => $email, 'password' => sha1($name))); Maybe there's a bug with set method. Btw there is an error in the password field set. password is with 2 S'sses in your table. Commented Jul 5, 2012 at 8:31
  • Thanks for help, i have amended the wrong spelling and inserted the array but still no luck. could it be something to do with the way the function is called? Commented Jul 5, 2012 at 9:03
  • 1
    add to the __construct function in the model: parent::__construct(); Commented Jul 5, 2012 at 9:06
  • Tried all suggestions and still nothing. Also now that i've made some adjustments, when i call the function in my controller $this->add_client_model->add_user(); i am getting a blank page and any echo's/print_r's i call are not displayed. Commented Jul 5, 2012 at 10:46

4 Answers 4

1

Try This:

$data = array(
  'first_name' => $fname,
  'second_name' => $sname,
  'email_address' => $email,
  'password' => sha1($password)
);

$this->db->insert('users', $data);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply! Unfortunately still returning variable names instead of value.
For debug add" print_r($data); exit; after $data variable.
0

Hmmm.... I have had sporadic issues sometimes too, but seems on in CI 2...

However, how about using the good old $_POST object to access it? this is after all where CI will get it's data.

Comments

0

Have you checked the variable values using echo or using firephp (if you're using it) ?

Ensure that the parameters send the appropriate value to the model as it accepts from the post.

Comments

0

I can't see the form helper in your controller. Perhaps you already autoloaded it though? If not, try adding it in there.

$this->load->helper('form');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.