在Codeigniter中编辑和删除

时间:2013-12-04 03:20:11

标签: php codeigniter hyperlink edit crud

我创建了一个包含搜索功能的表,该表显示了我数据库中的信息。我已经在每行的最后2列上创建了一个编辑和删除链接。 问题是,当我点击编辑它时,不会将我引导到我创建的表单并将其链接到,对于删除链接,它根本不会删除。我将包含视图的代码,如果您需要我再显示代码,我会这样做。

<table  align="center">
<tr>
    <th>Voter #</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Middle</th>
    <th>Home #</th>
    <th>Street</th>
    <th>Apt</th>
    <th>Zip</th>
    <th>DOB</th>
    <th>District</th>
    <th>Edit</th>
    <th>Delete</th>

</tr>
<?php 
foreach($query as $voter){
    $voter_id = $voter['voterNum'];
    ?>
<tr align="center">
    <td><?php echo $voter['voterNum'] ?></td>
    <td><?php echo $voter['firstName'] ?></td>
    <td><?php echo $voter['lastName'] ?></td>
    <td><?php echo $voter['midInitial'] ?></td>
    <td><?php echo $voter['homeNum'] ?></td>
    <td><?php echo $voter['street'] ?></td>
    <td><?php echo $voter['apt'] ?></td>
    <td><?php echo $voter['zip'] ?></td>
    <td><?php echo $voter['dob'] ?></td>
    <td><?php echo $voter['district'] ?></td>
    <td><a href= "<?php echo base_url("reg/edit_voter/");?><?php echo $voter_id ?>">Edit</a></td>
    <td><?php echo anchor('reg/delete_voter'.$voter_id, 'Delete', 
            array('onClick' => "return confirm('Are you sure you want to delete?')"));
    ?>
    </td>
</tr>
<?php
}
?>
</table>

更新  的控制器

public function search(){
    $search_term = array(
    'firstName' => $this->input->post('firstName'),
    'lastName' => $this->input->post('lastName'),
    'street' => $this->input->post('street'),
    'dob' => $this->input->post('dob')
        );


    $data['query'] = $this->reg_model->search_voters($search_term);


    $this->load->view("reg_header");
    $this->load->view("reg_nav");
    $this->load->view("reg_search", $data); 
    }
    function edit_voter($voter_id) {

    $voter = $this->reg_model->get_voter($voter_id);

    $this->data['title'] = 'Edit Voter';

    $this->load->library("form_validation");

    $this->form_validation->set_rules("firstName", "First Name", "required");
    $this->form_validation->set_rules("lastName", "Last Name", "required");
    $this->form_validation->set_rules("homeNum", "Home Number", "required");
    $this->form_validation->set_rules("street", "Street", "required");
    $this->form_validation->set_rules("zip", "Zip Code", "required");
    $this->form_validation->set_rules("dob", "Date of Birth", 'trim|required|valid_date[d/m/y,/]');
    $this->form_validation->set_rules("district", "District", "required");

    if (isset($_POST) && !empty($_POST))
    {       
        $data = array(
            'firstName' => $this->input->post('firstName'),
            'lastName' => $this->input->post('lastName'),
            'midInitial' => $this->input->post('midInitial'),
            'homeNum' => $this->input->post('homeNum'),
            'street' => $this->input->post('street'),
            'apt' => $this->input->post('apt'),
            'zip' => $this->input->post('zip'),
            'dob' => $this->input->post('dob'),
            'district' => $this->input->post('district')
        );

        if ($this->form_validation->run() === true)
        {
            $this->reg_model->update_voter($voter_id, $data);

            $this->session->set_flashdata('message', "<p>voter updated successfully.</p>");

            redirect(base_url().'reg/search/'.$voter_id);
        }           
    }

    $this->data['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));

    $this->data['voter'] = $voter;


    $this->data['firstName'] = array(
        'name'      => 'firstName',
        'id'        => 'firstName',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('firstName', $voter['firstName'])
    );

    $this->data['lastName'] = array(
        'name'      => 'lastName',
        'id'        => 'lastName',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('lastName', $voter['lastName'])
    );

    $this->data['midInitial'] = array(
        'name'      => 'midInitial',
        'id'        => 'midInitial',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('midInitial', $voter['firstName'])
        );

    $this->data['homeNum'] = array(
        'name'      => 'homeNum',
        'id'        => 'homeNum',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('homeNum', $voter['homeNum'])
        );


    $this->data['street'] = array(
        'name'      => 'street',
        'id'        => 'street',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('street', $voter['street'])
    );

    $this->data['apt'] = array(
        'name'      => 'apt',
        'id'        => 'apt',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('apt', $voter['apt'])
    );

    $this->data['zip'] = array(
        'name'      => 'zip',
        'id'        => 'zip',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('zip', $voter['zip'])
    );

    $this->data['dob'] = array(
        'name'      => 'dob',
        'id'        => 'dob',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('dob', $voter['dob'])
    );

    $this->data['district'] = array(
        'name'      => 'district',
        'id'        => 'district',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('district', $voter['district'])
    );


    $this->load->view('edit_voter_form', $this->data);
}    

    function delete_voter($voter_id) {
    $this->reg_model->del_voter($voter_id);

    $this->session->set_flashdata('message', '<p>Product were successfully deleted!</p>');

    redirect(base_url('reg/search'));
}

1 个答案:

答案 0 :(得分:2)

为什么要混合使用base_url()和anchor()?只需使用anchor()。

<td><?php echo anchor("reg/edit_voter/{$voter_id}", 'Edit') ?></td>


<td><?php echo anchor('reg/delete_voter/'.$voter_id, 'Delete', 
            array('onClick' => "return confirm('Are you sure you want to delete?')"));
    ?>
</td>

我认为你的删除链接缺少一个尾部斜杠。