当尝试更新记录时,它给出的值为0 - codeigniter

时间:2013-04-22 07:48:42

标签: php mysql codeigniter sql-update crud

我正在开发一个用于添加工厂的小型系统。 但现在当我点击编辑并填写表格时(仍然是空的,因为我没有设置set_value) 并单击“提交”,它为所有字段赋值0.

我认为它与id有关,但我不知道问题是什么。

我指向编辑页面的链接(使用id作为段(3))

<a href="<?= base_url();?>members/updatebedrijven/<?= $item->idbedrijven;?>">Edit</a>

我的控制器功能:

function updatebedrijven()
{
    $this->load->view('members/header');
    $this->load->view('members/editform');
    $this->load->view('members/footer');
}

function update()
{
    $this->members_model->updatebedrijf($id);
    redirect('members/index');
}

我的模特功能:

function updatebedrijf($id)
{
    $id = $this->uri->segment(3);
    $data = array(
       'Bedrijfsnaam' => $this->input->post('Bedrijfsnaam'),
       'Postcode' => $this->input->post('Postcode'),
       'Plaats' => $this->input->post('Plaats'),
       'Telefoonnummer' => $this->input->post('Telefoonnummer'),
       'Email' => $this->input->post('Email'),
       'Website' => $this->input->post('Website'),
       'Profiel' => $this->input->post('Profiel'),
       'Adres' => $this->input->post('Adres'),
       'logo' => $this->input->post('logo')
    );

    $this->db->where('idbedrijven', $id);
    $this->db->update('bedrijven', $data); 
}

我的表格:

<div id="cpanel">
<h2>Bedrijfsgegevens aanpassen</h2>
<?= br(1);?>
<?= validation_errors(); ?>
<?= form_open('members/update');?>
<table>
<b>NAW Gegevens</b>
    <tr>
    <td><?= form_label('Bedrijfsnaam:');?></td>
    <td><?= form_input('Bedrijfsnaam');?></td>
    </tr>

    <tr>
    <td><?= form_label('Adres:');?></td>
    <td><?= form_input('Adres');?></td>
    </tr>

    <tr>
    <td><?= form_label('Postcode:');?></td>
    <td><?= form_input('Postcode');?></td>
    </tr>

    <tr>
    <td><?= form_label('Plaats:');?></td>
    <td><?= form_input('Plaats');?></td>
    </tr>

    <tr>
    <td><?= form_label('Telefoonnummer:');?></td>
    <td><?= form_input('Telefoonnummer');?></td>
    </tr>

    <tr>
    <td><?= form_label('Website:');?></td>
    <td><?= form_input('Website');?></td>
    </tr>

    <tr>
    <td><?= form_label('Email:');?></td>
    <td><?= form_input('Email');?></td>
    </tr>

    <tr>
    <td><?= form_label('Profiel:');?></td>
    <td><?= form_textarea('Profiel');?></td>
    </tr>

    <tr>
    <td><?= form_submit('submit', 'Opslaan');?> <?= form_reset('reset', 'Reset');?></td>
    </tr>


</table>
<?= form_close()?>
<br/>
<a href="<?= base_url();?>members/index"><< Terug</a>

</div>

1 个答案:

答案 0 :(得分:1)

首先,我们需要移动一些代码。您不应该访问模型中的uri或post数据,因此我们将其移出控制器。

控制器功能:

function updatebedrijven()
{
    // Access the uri in the update form and pass the value to the view
    $data['id'] = $this->uri->segment(3);

    $this->load->view('members/header');
    $this->load->view('members/editform', $data);
    $this->load->view('members/footer');
}

function update()
{

    // Access the post data in the function the form is posted to (not the model)
    $data = array(
      'Bedrijfsnaam' => $this->input->post('Bedrijfsnaam'),
      'Postcode' => $this->input->post('Postcode'),
      'Plaats' => $this->input->post('Plaats'),
      'Telefoonnummer' => $this->input->post('Telefoonnummer'),
      'Email' => $this->input->post('Email'),
      'Website' => $this->input->post('Website'),
      'Profiel' => $this->input->post('Profiel'),
      'Adres' => $this->input->post('Adres'),
      'logo' => $this->input->post('logo')
    );
    $this->members_model->updatebedrijf($id);
    redirect('members/index');
}

模特功能:

function updatebedrijf($id, $data)
{
    $this->db->where('idbedrijven', $id);
    $this->db->update('bedrijven', $data); 
}

其次,当我们需要将表单发布到members/update时,您将表单发布到members/update/$id。因此,让我们稍微更改一下视图代码,并包含从控制器传递给它的id变量:

<?= form_open('members/update/'.$id);?>