Welcome Guest, Not a member yet? Register   Sign In
Edit db record - Review code
#1

[eluser]weetstraw[/eluser]
How does this look? This code generates the form list, fills the fields with the db content and then updates. Am I missing anything?
model
Code:
/* Get Item */
function getItem($id)
{
    $query=$this->db->get_where('content', array('id'=>$id));
    
    if($query->num_rows()){
        $row=$query->row();
        
        return $row;
    }
}

/* Update Record */
function updateRecord($id)
{    
    $this->db->where('id',$id);
    $this->db->update('content',$_POST);
    redirect("home/article/$id");
}

controller
Code:
/* Edit Record */
function edit($id)
{
    $id=$this->uri->segment(3);
    
    $data['title']=" - Manage Content - Edit Content";
    $data['heading']="Edit Record";
    $data['formAction']="manage/updateRecord/$id";
    
    $row=$this->Home_model->getItem($id);

    $data['formField']=array(
        form_input(array('name'=>'title','value'=>$row->title,'size'=>'50')),
        form_input(array('name'=>'link','value'=>$row->link,'size'=>'50')),
        form_input(array('name'=>'tags','value'=>$row->tags,'size'=>'50')),
        form_textarea(array('name'=>'description','value'=>$row->description,'cols'=>'45','rows'=>'20'))
    );
    
    $this->load->view('form_view',$data);
}
#2

[eluser]pistolPete[/eluser]
Some suggestions:

function getItem
If there is no entry for $id, nothing is returned. I'd suggest returning FALSE and checking that value in the controller.

function updateRecord
Don't use the whole $_POST array, it could break your query if there are values submitted via POST not contained in the database structure. Rather use the Input Class
The model should not redirect, leave that to the controller.

I'd put the form related functions (form_input, form_textarea) into the view.

Don't you validate the entered values? You should consider using the Form Validation Class.




Theme © iAndrew 2016 - Forum software by © MyBB