Note that there are some explanatory texts on larger screens.

plurals
  1. POcodeigniter method get losing data on controller
    primarykey
    data
    text
    <p>i have aa application where i can edit fields of a database table. Everything is fine and the data is updated, except if i go to the field that i want to update and instead of changing the text, put the field blank. In that way i get php errors because (i guess) the method of my controller lose the id of that row of the table:</p> <p>I would be glad if anyone could help me, thank's in advance.</p> <p>Part of the view file where i use the method get to pass the id of the table row (edit_fuga.php).</p> <pre><code> &lt;div id='tab2'&gt; &lt;?php $query = $this-&gt;db-&gt;query("SELECT * FROM fugas WHERE tipo_fuga='2';"); foreach ($query-&gt;result_array() as $row) { echo "&lt;input type='hidden' name='id_fuga' value='". $row['id_fuga']."'&gt;"; echo $row['desc_fuga']."&amp;nbsp;&amp;nbsp;&lt;a class='button' href='http://localhost/code/index.php/fugas/editar_fuga?var1=".$row['id_fuga']."'&gt;Editar&lt;/a&gt;"; echo "&lt;br/&gt;"; } ?&gt; &lt;/div&gt; </code></pre> <p>The view to where i am redirected (editar_fuga.php):</p> <pre><code>&lt;?php echo validation_errors(); ?&gt; &lt;?php echo form_open('fugas/editar_fuga') ?&gt; &lt;?php $query = $this-&gt;db-&gt;query("SELECT desc_fuga_table FROM tipos_fuga where tipo_fuga_table='".$fuga_selected['tipo_fuga']."';"); foreach ($query-&gt;result() as $row) { echo $row-&gt;desc_fuga_table; } $tipo_fuga_selected=$row-&gt;desc_fuga_table; ?&gt; &lt;label for="tipo_fuga"&gt;&lt;?php echo $tipo_fuga_selected; ?&gt;&lt;/label&gt;&lt;br/&gt; &lt;p/&gt; &lt;label for="desc_fuga"&gt;Desc da fuga:&lt;/label&gt;&lt;p/&gt; &lt;textarea rows="4" cols="50" name="desc_fuga"&gt;&lt;?php echo $fuga_selected['desc_fuga']; ?&gt;&lt;/textarea&gt;&lt;p/&gt; &lt;input type="hidden" name="id_fuga" value="&lt;?php echo $fuga_selected['id_fuga']; ?&gt;"&gt; &lt;input type="submit" name="submit" value="Alterar Fuga" /&gt;&lt;p/&gt; &lt;INPUT Type="button" VALUE="Voltar" onClick="history.go(-1);return true;"&gt; &lt;/form&gt; </code></pre> <p>The method on the controller of the view editar_fuga.php</p> <pre><code>public function editar_fuga(){ $fuga_id = $this-&gt;input-&gt;get('var1'); var_dump($fuga_id); $this-&gt;load-&gt;helper('form'); $this-&gt;load-&gt;library('form_validation'); //$this-&gt;load-&gt;view('templates/header'); $this-&gt;form_validation-&gt;set_rules('desc_fuga', 'Descrição da fuga acidental', 'required'); if ($this-&gt;form_validation-&gt;run() === FALSE) { $data['fuga_selected'] = $this-&gt;fugas_model-&gt;get_fuga($fuga_id); $this-&gt;load-&gt;view('fugas/editar_fuga', $data); } else { $this-&gt;fugas_model-&gt;update_fuga(); echo "Fuga alterada com sucesso.&lt;br/&gt;&lt;br/&gt;"; $this-&gt;load-&gt;view('fichas/index'); } } </code></pre> <p>Fuga Model:</p> <pre><code> public function update_fuga(){ $data = array( 'desc_fuga' =&gt; $this-&gt;input-&gt;post('desc_fuga') ); $id=$this-&gt;input-&gt;post('id_fuga'); $this-&gt;db-&gt;where('id_fuga', $id); $this-&gt;db-&gt;update('fugas', $data); } public function get_fuga($fuga_id){ var_dump($fuga_id); $query = $this-&gt;db-&gt;query("SELECT * FROM fugas where id_fuga='".$fuga_id."';"); foreach ($query-&gt;result_array() as $row) { $data_fuga = array( 'id_fuga' =&gt; $row['id_fuga'], 'tipo_fuga' =&gt; $row['tipo_fuga'], 'desc_fuga' =&gt; $row['desc_fuga'] ); } return $data_fuga; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload