2
votes

Hey guys I'm having trouble running an update query through a form in codeigniter. There is no error returned, but the query does not update in the database. It should be noted there's another form on the same page running an insert query that works fine.

View:

            <?php
                $modify = array(
                    'id' => 'modify-form', 
                    'class' => ''                       
                );
                echo form_open('main/modify_idea', $modify); 
                $ta_modify = array(
                    'id' => $row->id,
                    'name' => 'ta_modify', 
                    'label' => 'ta_modify', 
                    'placeholder' => $row->idea, 
                    'method' => 'Post'
                ); 
                echo form_textarea($ta_modify); 
                echo form_hidden('facebook_id', $session['id']);
                echo "<div id='button'>"; 
                echo form_submit('submit', 'Post'); 
                echo "</div>";
                echo form_close(); 
            ?>

Controller:

public function modify_idea() {
    $this->load->model('idea'); 
    $this->idea->modify_idea(); 
    redirect('main/members'); 
}

Model:

public function modify_idea() {
    $data = array( 
        'id' => $this->input->post('id'), 
        'idea' => $this->input->post('ta_modify'), 
        'facebook_id' => $this->input->post('facebook_id')
    );

    $query = $this->db->query("UPDATE idea SET `idea` = ? WHERE `id` =?", array($data['idea'], $data['id'])); 
    return $query; 
}

I've utilized the following resources but still no luck:
CodeIgniter MySQL query not working

CodeIgniter MySQL Query not returning any data, even though there definitely is data to be returned!

http://www.tutorialspoint.com/mysql/mysql-update-query.htm
https://www.codeigniter.com/userguide3/database/query_builder.html#deleting-data

As always, thanks for the help!

2
Ok, i'm not sure about what you are doing. but I think that your problem is either in the redirect or in using post inside model. the redirect might be killing your process and redirects before completing and the model post I'm not sure that this is how codeigniter works even it's not a problem through mvc but might be a problem with codeigniter structure - mamdouh alramadan
I passed 'id' as a hidden variable and that fixed it. Thanks for your help! - Michael F
please mark your answer as solved (you'll have to say you solved it yourself in this case) so others don't think you still need help. Thanx! - jmadsen

2 Answers

0
votes

Pass your post data from controller to model

Change your controller code:

public function modify_idea() {
    $this->load->model('idea');
     $data = array( 
        'id' => $this->input->post('id'), 
        'idea' => $this->input->post('ta_modify'), 
        'facebook_id' => $this->input->post('facebook_id')
    );
    $this->idea->modify_idea($data); 
    redirect('main/members'); 
}

in model do this

public function modify_idea($data) {
   $query = $this->db->query("UPDATE idea SET `idea` = ? WHERE `id` =?",  
                           array($data['idea'], $data['id'])); 
   return $query; 
}
0
votes

Please pass your post values to model from controller. Then it will work.