2

嘿伙计们,我在通过 codeigniter 中的表单运行更新查询时遇到问题。没有返回错误,但查询不会在数据库中更新。应该注意的是,在同一页面上还有另一个表单运行了一个可以正常工作的插入查询。

看法:

            <?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(); 
            ?>

控制器:

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

模型:

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; 
}

我使用了以下资源,但仍然没有运气:
CodeIgniter MySQL query not working

CodeIgniter MySQL Query 不返回任何数据,即使肯定有要返回的数据!

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

一如既往,感谢您的帮助!

4

2 回答 2

0

请将您的帖子值从控制器传递给模型。然后它将起作用。

于 2014-07-07T07:33:52.040 回答
0

将您的发布数据从控制器传递到模型

更改您的控制器代码:

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'); 
}

在模型中这样做

public function modify_idea($data) {
   $query = $this->db->query("UPDATE idea SET `idea` = ? WHERE `id` =?",  
                           array($data['idea'], $data['id'])); 
   return $query; 
}
于 2013-02-12T05:53:53.093 回答