Welcome Guest, Not a member yet? Register   Sign In
Stuck with a simple update
#1

[eluser]Dizza[/eluser]
Hi all,

Im stuck, ive been struggling with this problem for almost 2 hours now and still cant/dont have the solution:

I want to update a form field, but it just wont update, it seems that it doenst know what to update? Maybe a other pair of eyes can find the error! Big Grin

<b>Controller</b>
Code:
function update_kandidaten()
    {    
        $data = array(
               'functie' =>$this->input->post('functie'),    
               'kandidaatnr' => $this->input->post('kandidaatnr'),    
               'branche' => $this->input->post('branche'),    
           'regio' => $this->input->post('regio'),    
               'dienstverband' => $this->input->post('dienstverband'),
               'beschrijving' => $this->input->post('beschrijving')
            );

       $this->admin_model->werknemer_update($data);
}

<b>Model</b>
Code:
function werknemer_update($data)
    {
     $this->db->where('id',  $this->uri->segment(3));
     $this->db->update('kandidaten',$data);
     return;
}

<b>View</b>
Code:
&lt;?php echo form_open_multipart('admin/update_kandidaten');?&gt;

&lt;?php
$segs = $this->uri->segment(3, 0);
$query = $this->db->get_where('kandidaten', array('id' => $segs));

foreach ($query->result() as $row)
{ ?&gt;

    <p>
      <label for="functie">Functie:</label>
       &lt;input type="text" name="functie" id="functie" value="&lt;?php echo $row-&gt;functie;?&gt;" />
    </p>
    <p>
      <label for="branche">Branche:</label>
         &lt;input type="text" name="branche" id="branche" value="&lt;?php echo $row-&gt;branche;?&gt;" />
    </p>
    <p>
      <label for="regio">Regio:</label>
           &lt;input type="text" name="regio" id="regio" value="&lt;?php echo $row-&gt;regio;?&gt;" />
    </p>
    
     <p>
      <label for="dienstverband">Dienstverband:</label>
          &lt;input type="text" name="dienstverband" id="dienstverband" value="&lt;?php echo $row-&gt;dienstverband;?&gt;" />
    </p>
    
    <p>
      <label for="kandidaatnr">kandidaatnr:</label>
          &lt;input type="text" name="kandidaatnr" id="kandidaatnr" value="&lt;?php echo $row-&gt;kandidaatnr;?&gt;"  />
    </p>
    
  

    <p><label for="beschrijving">Beschrijving:</label>
      &lt;textarea cols="40" rows="10" name="beschrijving" id="beschrijving"&gt; &lt;?php echo $row->beschrijving;?&gt;
      &lt;/textarea&gt;
    </p>  
    &lt;?php
}
?&gt;
  &lt;?php echo form_submit('Aanpassen', 'Vacature aanpassen'); ?&gt;
&lt;?php echo form_close(); ?&gt;

Thanks!
#2

[eluser]cahva[/eluser]
You have atleast 2 things wrong. Your update method depends on url segment where id is the last but the form's action does not have that. Second, you are dealing with unique id so its pointless to loop through the result when theres only one row. Even if there were more than one result, the form would not work because the inputs have the same name and would only send the last row's values. Also try to keep the $this->db->.. off from your view. Fetch the result from your model in a controller and pass it to the view. Something like this(simplified):

Controller(2 methods, update and view)
Code:
function view_kandidaten($id=FALSE)
{
    if (!$id)
    {
        return FALSE;
    }
    
    $data['id'] = $id;
    $data['row'] = $this->admin_model->get_kandidaten($id);
    
    $this->load->view('view_kandidaten',$data);
}


function update_kandidaten()
{
    if (!$this->input->post('id'))
    {
        return FALSE;
    }
    
    $id = $this->input->post('id');
    
    $data = array(
        'functie' =>$this->input->post('functie'),    
        'kandidaatnr' => $this->input->post('kandidaatnr'),    
        'branche' => $this->input->post('branche'),    
        'regio' => $this->input->post('regio'),    
        'dienstverband' => $this->input->post('dienstverband'),
        'beschrijving' => $this->input->post('beschrijving')
    );

    $this->admin_model->werknemer_update($id,$data);
    redirect('admin/view_kandidaten/'.$id);
}

Model
Code:
function werknemer_update($id,$data)
{
    $this->db->where('id',  $id);
    $this->db->update('kandidaten',$data);
    return;
}

function get_kandidaten($id)
{
    return $this->db->where('id',$id)->get('kandidaten')->row();
}

View (view_kandidaten)
Code:
&lt;?php echo form_open('admin/update_kandidaten'); // Changed to plain form_open, you're not uploading files here ?&gt;
&lt;?php echo form_hidden('id',$id) ?&gt;


    <p>
      <label for="functie">Functie:</label>
       &lt;input type="text" name="functie" id="functie" value="&lt;?php echo $row-&gt;functie;?&gt;" />
    </p>
    <p>
      <label for="branche">Branche:</label>
         &lt;input type="text" name="branche" id="branche" value="&lt;?php echo $row-&gt;branche;?&gt;" />
    </p>
    <p>
      <label for="regio">Regio:</label>
           &lt;input type="text" name="regio" id="regio" value="&lt;?php echo $row-&gt;regio;?&gt;" />
    </p>
    
     <p>
      <label for="dienstverband">Dienstverband:</label>
          &lt;input type="text" name="dienstverband" id="dienstverband" value="&lt;?php echo $row-&gt;dienstverband;?&gt;" />
    </p>
    
    <p>
      <label for="kandidaatnr">kandidaatnr:</label>
          &lt;input type="text" name="kandidaatnr" id="kandidaatnr" value="&lt;?php echo $row-&gt;kandidaatnr;?&gt;"  />
    </p>
    
  

    <p><label for="beschrijving">Beschrijving:</label>
      &lt;textarea cols="40" rows="10" name="beschrijving" id="beschrijving"&gt; &lt;?php echo $row->beschrijving;?&gt;
      &lt;/textarea&gt;
    </p>  

&lt;?php echo form_submit('Aanpassen', 'Vacature aanpassen'); ?&gt;
&lt;?php echo form_close(); ?&gt;

There can be errors in this as I did not test it naturally.
#3

[eluser]Dizza[/eluser]
[quote author="cahva" date="1300236234"].....[/quote]

Thanks! lol @ my form_open_multipart. I think i almost got it to work now! There is only one error left. The error comes from my view file, it says undefined variable about the id and row.

Code:
Severity: Notice
Message: Undefined variable: id
Filename: views/aanpassen_kandidaten_view.php
Line Number: 88

can it be because i still echo the data in a $row?
Code:
&lt;?php echo $row->functie;?&gt;

thx for all your help!
#4

[eluser]CroNiX[/eluser]
[quote author="Dizza" date="1300244698"][quote author="cahva" date="1300236234"].....[/quote]

Code:
Severity: Notice
Message: Undefined variable: id
Filename: views/aanpassen_kandidaten_view.php
Line Number: 88

can it be because i still echo the data in a $row?
Code:
&lt;?php echo $row->functie;?&gt;

thx for all your help![/quote]
No, it would be from the 2nd line in your view:
&lt;?php echo form_hidden('id',$id) ?&gt;
You are using $id, but it is complaining that it isn't defined. You probably aren't passing it in from your controller to the view.




Theme © iAndrew 2016 - Forum software by © MyBB