Codeigniter更新不起作用

时间:2014-02-04 21:39:35

标签: php database codeigniter

我无法让我的更新正常运行。删除,查看和添加工作正常。

这是我的extraoptie_model:

function update($id, $extraOptie) {
    $this->db->where('id', $id);
    $this->db->update('extraOptie', $extraOptie);
}

我的控制员:

    function update($id) {
    //  Check admin if not Acces Denied
    $gebruiker = $this->session->userdata('gebruiker');
    if ($gebruiker->accountLevel != "admin") {
        $data['title'] = 'Acces Denied';
        $partials = array('header' => 'main_header', 'content' => 'admin_noacces', 'footer' => 'main_footer');
        $this->template->load('main_master', $partials, $data);
    } else {
        // prefill form values
        $extraOptie = $this->extraoptie_model->get($id);
        $data['id'] = $id;
        $data['beschrijving'] = $extraOptie->beschrijving;
        $data['actuelePrijs'] = $extraOptie->actuelePrijs;
        $data['soort'] = $extraOptie->soort;
        $data['aantalGangen'] = $extraOptie->aantalGangen;
        $data['tabelNaam'] = $extraOptie->tabelNaam;

        // set common properties
        $data['title'] = 'Extra optie update';
        $data['message'] = '';
        $data['action'] = site_url('admin/extraoptie/updateExtraOptie/');

        //  load view
        $partials = array('header' => 'main_header', 'content' => 'admin_extraoptieedit', 'footer' => 'main_footer');
        $this->template->load('main_master', $partials, $data);
    }
}

function updateExtraOptie() {
    //  Check admin if not Acces Denied
    $gebruiker = $this->session->userdata('gebruiker');
    if ($gebruiker->accountLevel != "admin") {
        $data['title'] = 'Acces Denied';
        $partials = array('header' => 'main_header', 'content' => 'admin_noacces', 'footer' => 'main_footer');
        $this->template->load('main_master', $partials, $data);
    } else {
        // set common properties
        $data['title'] = 'Update extra optie';
        $data['action'] = site_url('admin/extraoptie/updateExtraOptie');


        // save data
        $id = $this->input->post('id');
        $extraOptie = array('beschrijving' => $this->input->post('beschrijving'),
            'actuelePrijs' => $this->input->post('actuelePrijs'),
            'soort' => $this->input->post('soort'),
            'aantalGangen' => $this->input->post('aantalGangen'),
            'tabelNaam' => $this->input->post('tabelNaam'));

        if ($extraOptie['aantalGangen'] == '') {
            $extraOptie['aantalGangen'] = null;
        }
        if ($extraOptie['tabelNaam'] == '') {
            $extraOptie['tabelNaam'] = null;
        }

        $this->extraoptie_model->update($id, $extraOptie);

        // set user message
        $data['message'] = '<div class="success">update success</div>';


        //  redirect to index
        redirect('admin/extraoptie/index', 'refresh');
    }
}

我的观点:

<table class='centered text'>
    <form method="post" action="<?php echo $action; ?>">
    <?php
    $hidden = array('id' => $id);
    form_hidden($hidden);
    ?>
    <tr>
        <td><?php echo form_label('Beschrijving:', 'beschrijving'); ?></td>
        <td><?php echo form_textarea(array('name' => 'beschrijving', 'id' => 'beschrijving', 'value' => $beschrijving, 'cols' => '30', 'rows' => '5')); ?></td>
    </tr>
    <tr>   
        <td><?php echo form_label('Prijs:', 'prijs'); ?></td>
        <td><?php echo form_input(array('name' => 'actuelePrijs', 'id' => 'actuelePrijs', 'value' => $actuelePrijs, 'size' => '10')); ?></td>
    </tr>
    <tr>
        <td><?php echo form_label('Soort:', 'soort'); ?></td>
        <td><?php echo form_input(array('name' => 'soort', 'id' => 'soort', 'value' => $soort, 'size' => '30')); ?></td>
    </tr>
    <tr>
        <td><?php echo form_label('Aantal gangen:', 'aantalGangen'); ?></td>
        <td><?php echo form_input(array('name' => 'aantalGangen', 'id' => 'aantalGangen', 'value' => $aantalGangen, 'size' => '10')); ?></td>
    </tr>
    <tr>
        <td><?php echo form_label('Tabel naam:', 'tabelNaam'); ?></td>
        <td><?php echo form_input(array('name' => 'tabelNaam', 'id' => 'tabelNaam', 'value' => $tabelNaam, 'size' => '30')); ?></td>
    </tr>    
    <tr>
        <td colspan='2' class='center'>
            <?php echo form_submit('submit', 'Opslaan', 'id="opslaan"');; ?>
            <?php echo form_button('annuleer', 'Annuleren', 'id="annuleer"'); ?>
            <?php form_close(); ?>
        </td>
    </tr>    
</table>

欢迎所有帮助已经工作了几个小时,似乎无法找到问题。

Grtz Nella

3 个答案:

答案 0 :(得分:0)

可能问题来自于这样的事实 你有一个php form_close(); 但是没有php form_open(); 不确定CI如何在没有开放的情况下对收盘做出反应。

答案 1 :(得分:0)

您是否启用了CSRF保护?如果是这样,您将需要使用form_open(),如史蒂夫所说,或者使用您的csrf令牌添加隐藏输入,例如:

 <input type="hidden" name="<?php echo $this->security->get_csrf_hash();?>" value="<?php echo $this->security->get_csrf_token_name();?>"/>

答案 2 :(得分:0)

感谢您的快速回复!

在与朋友一起盯着代码再过几个小时后,我发现了错误。

问题是我没有回显form_hidden($ hidden)所以没有id传递给更新模型。

<table class='centered text'>
<form method="post" action="<?php echo $action; ?>">
<?php
$hidden = array('id' => $id);
form_hidden($hidden);
?>
<tr>

所以它必须是:

<table class='centered text'>
<form method="post" action="<?php echo $action; ?>">
<?php
$hidden = array('id' => $id);
echo form_hidden($hidden);
?>
<tr>