开发者

CodeIgniter query not being inserted into database

Here is my function:

function create_member()
    {

        $new_member_insert_data = array(
            'first' => $this->input->post('first_name'),
            'last' => $this->input->post('last_name'),
            'email' => $this->input->post('email_address'),         
            'username' => $this->input->post('username'),
            'password' => $this->input->post('password')                        
        );

        $insert = $this->db->insert('members', $new_member_insert_data);
        return $insert;
    }

My table is named "members" and I can connect to my database to pull out data I manually type into it. Any thoughts about why this doesn't insert my data? All of the field names are correct and the data is in the POST. I echoed it to check out both of these things. Here is my form:

echo form_open('engineering-resources/login/create');

echo form_input('first_name', set_value('first_name', 'First Name'));
echo form_input('last_name', set_value('last_name', 'Last Name'));
echo form_input('email_address', set_value('email_address', 'Email Address'));
?>
</fieldset>

<fieldset>
<legend>Login Info</legend>
<?php
echo form_input('username', set_value('username', 'Username'));
echo form_input('password', set_value('password', 'Password'));
echo form_input('password2', 'Password Confirm');

echo form_submit('submit', 'Create Acccount');
?>

There is a route for the form action: $route['engineering-resources/login/create'] = "resources/login/create_member";

Here is my controller:

function create_member()
    {
        $this->load->library('form_validation');
        echo 'hello';
        // field name, error message, validation rules
        $this->form_validation->set_rules('first_name', 'Name', 'trim|required');
        $this->form_val开发者_如何学编程idation->set_rules('last_name', 'Last Name', 'trim|required');
        $this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
        $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');


        if($this->form_validation->run() == FALSE)
        {
            $data['title'] = 'Please Resubmit Your Information';
            $data['main_content'] = 'resources/signup_form';
            $this->load->view('templates/main.php', $data);
        }

        else
        {           
            $this->load->model('login/membership_model');
            echo $this->db->last_query();

            if($query = $this->membership_model->create_member())
            {
                $data['title'] = 'User Area';
                $data['main_content'] = 'resources/logged_in_area';
                $this->load->view('templates/main', $data);
            }
            else
            {
                $this->load->view('resources/signup_form');         
            }
        }

    }


You can enable profiling by using the following directive:

$this->output->enable_profiler(TRUE);

Then you can see if your query is executed and if it's correct.


Hmmm, try to prepare your array in the controller:

$this->load->model('login/membership_model');
$new_member_insert_data = array(
    'first' => $this->input->post('first_name'),
    'last' => $this->input->post('last_name'),
    'email' => $this->input->post('email_address'),         
    'username' => $this->input->post('username'),
    'password' => $this->input->post('password')                        
);
if($query = $this->membership_model->create_member($new_member_insert_data))

And your model:

function create_member($data_arr)
{
    $insert = $this->db->insert('members', $data_arr);
    return $insert;
}


Maybe I'm going to learn something myself here, but you are using a syntax I'm not familiar with in one of your if() statements, so I'm going to bring it up to see if it helps. Feel free to educate me if I'm wrong.

I assume your first create_member() function is in your membership_model, and that the controller function you exhibit is the one you re-route to: 'resources/login/create_member'.

In your controller you use:

if($query = $this->membership_model->create_member())

and I don't see $query defined anywhere, so I expect php to be comparing the returned boolean from the create_member() function to an undefined variable: $query. Maybe this is a common way to assign a variable and run a function all at once (that I'm unfamiliar with), but to trouble shoot, have you tried to call create_member() without this 'advanced' syntax?

$query = $this->membership_model->create_member();
if($query)
        {
            $data['title'] = 'User Area';
            $data['main_content'] = 'resources/logged_in_area';
            $this->load->view('templates/main', $data);
        }

I also agree with ifaour...you aren't technically passing anything to be inserted into membership_model/create_member(), so you may try to grab it in the controller function per his example. I'm not sure the POST array that is passed from the form to your controller gets passed to the model automatically.

These two steps might help you out. To recap: simplify your call to $this->membership_model->create_member($data); and put the POST array into an array ($data) in your controller.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜