0

I'm using CodeIgniter and getting undefined variable errors for every field I'm trying to insert into a database. Is there something I'm missing?

E: Oh yeah, and most importantly, it will insert "NULL" on all fields in the database after form is submitted.

Here's my view:

<?php $this->load->helper('form'); ?>
<?php $attributes = array(
        'id' => 'add_user_form'
    );
?>

<?php echo form_open('users/add_user', $attributes); ?>

        <span class="close_form_button_add_user_form">X</span>


    <div class="form_row">
    <input type="text" name="f_name" placeholder="Etunimi" class="half-width" />
    <input type="text" name="l_name" placeholder="Sukunimi" class="half-width" style="float: right; margin-right: 24px;"/>
    </div>

    <div class="form_row">
    <input type="email" name="email" placeholder="Uuden käyttäjän sähköpostiosoite" class="user full-width" />
    </div>

    <div class="form_row">
    <input type="email" name="email_confirm" placeholder="Sähköpostiosoite uudelleen" class="user full-width" />
    </div>

    <div class="form_row">
    <input type="text" name="phone_number" placeholder="Puhelinnumero (0401234567)" class="full-width" />
    </div>


    <div class="form_row" style="margin-top: 20px;">
    <select name="user_type" class="add_user_select" id="select_user_type">
    <option value="">Käyttäjätyyppi</option>
    <option value="admin">Hallinto</option>
    <option value="moderator">Hallinto 2</option>
    <option value="enduser">Pääkäyttäjä</option>
    </select>

    <input type="submit" name="add_user" value="Luo käyttäjä" class="login_submit" id="submit_user" style="margin: 5px 24px 0px 0px;"/>
    </div>

    <div class="form_row" id="add_user_optional_information">
    <select name="school" class="add_user_select half-width">
    <option value="">Koulutalo</option>
    <?php  foreach($schools as $school) :?>
        <option value="<?=$school['id']?>"><?=$school['name']?></option>
    <?php endforeach; ?>
    </select>

    <select name="subject" class="add_user_select half-width">
    <option value="">Laji</option>
    <?php  foreach($subjects as $subject) :?>
        <option value="<?=$subject['id']?>"><?=$subject['subject_name']?></option>
    <?php endforeach; ?>
    </select>
    </div>
     <?php echo form_close(); ?>


    <?php echo validation_errors(); ?>
    </div>

Here's my model for adding user into database (called user):

<?php

    class User extends CI_Model {

        public function add_user() {

            $data = array(
                    'username' => $username,
                    'name' => $name,
                    'email' => $email,
                    'password' => $password,
                    'user_type' => $user_type,
                    'phonenumber' => $phonenumber,
                    'school_id' => $school_id,
                    'subject_id' => $subject_id
                );

            $this->db->insert('users', $data);
        }
    }
?>

And my controller to handle the actual inserting: (ignore the comments)

    <?php

        class Users extends CI_Controller {
           public function add_user() 
           {

            if($this->input->post('add_user'))
            {

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

      $this->form_validation->set_rules('f_name', 'Etunimi', 'required|min_length[2]');
      $this->form_validation->set_rules('l_name', 'Sukunimi','required|min_length[3]');
      $this->form_validation->set_rules('email', 'Sähköpostiosoite','required|matches[email_confirm]|is_unique[users.email]|valid_email');
      $this->form_validation->set_rules('email_confirm', 'Sähköpostiosoite', 'required|valid_email');
      $this->form_validation->set_rules('phone_number', 'Puhelinnumero', 'min_length[7]|max_length[10]');


            if($this->input->post('user_type') == "moderator")
            {   

                    $this->form_validation->set_rules('school', 'Koulutalo', 'required');
                    $this->form_validation->set_rules('subject', 'Laji', 'required');
            }

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

                echo "Not working!!";
            }

            $first_part_username = substr($this->input->post('f_name'), 0, 2);
            $second_part_username = substr($this->input->post('l_name'), 0, 3);
            $random_number = random_string('numeric', 4);

            $username = $first_part_username . $second_part_username . $random_number;

            $password = random_string('alnum', 8);


            $this->load->model('user');

                $data = array (
                    'name'          => strtoupper($this->input->post('f_name')). " ".strtoupper($this->input->post('lname')),
                    'email'         => $this->input->post('email'),
                    'username'      => $username,
                    'password'      => $this->phpass->hash($password),
                    'user_type'     => $this->input->post('user_type'),
                    'phone_number'  => $this->input->post('phone_number'),
                    'school_id'     => $this->input->post('school_id'),
                    'subject_id'    => $this->input->post('subject_id')
                    );


                    if($this->user->add_user($data))
                    {
                        $this->load->library('email');

                        $config['newline'] = '\r\n';
                        $this->email->initialize($config);

                        $this->email->from('[email protected]', 'Your Name');
                        $this->email->to($this->input->post('email'));  
                        $this->email->subject('Käyttäjä rekisteröity');
                        $this->email->message(
                        "Hei," 
                        .strtoupper($this->input->post('f_name')). " ".strtoupper($this->input->post('l_name')).".
                        Tämä on automaattinen viesti kilpailuvastaavan toimesta. \n
                        Olemme luoneet sinulle palveluumme käyttäjätunnuksen ja salasanan, jonka avulla pääset käsiksi
                        infrastruktuurijärjestelmään. \n
                        Pääset kirjautumaan järjestelmään seuraavilla tunnuksilla:"

                        );  


                    if($this->email->send()) {

                        base_url().'dashboard';
                    }
                }

                }
                $this->load->view('header');
                $this->load->view('dashboard', $data);
                $this->load->view('footer');

           }
        }
     ?>

2 Answers 2

1

You are passing $data from Controller to your Model but you haven't defined any parameter you receiving in your Model. Change model function to this -

public function add_user($data) {

        $data = array(
                'username' => $data['username'],
                'name' => $data['name'],
                'email' => $data['email'],
                'password' => $data['password'],
                'user_type' => $data['user_type'],
                'phonenumber' => $data['phone_number'],
                'school_id' => $data['school_id'],
                'subject_id' => $data['subject_id']
            );

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

Comments

0

You are calling $this->user->add_user($data) but your model function definition is public function add_user() , it should be public function add_user( $data )

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.