Update Codeigniter

I am trying to do a UPDATE in the database using CodeIgniter, however the UPDATE is not running the way I would like.

What happens, I have 5 manuals with different contents, and when I do the UPDATE in this way it changes the content of all manuals together, and I want to change only the content of a certain manual, I think I have to somehow use the identification of each (id), but I have tried several ways and I managed to get to the expected result.

Below follows the code

HTML:

<form class="form-horizontal form-material" id="manual-form" method="post" action="<?php echo base_url('app/Manual/salvar'); ?>"> 
    <!--  Modal -->
    <div class="modal fade " id="myModal" style="padding-top: 0px;">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">

                <!-- Modal Cabecalho -->
                <div class="modal-header">
                    <h4 class="modal-title"><?php echo $servico_manual->titulo; ?></h4>
                    <button type="button" class="close" style="outline:0;-webkit-box-shadow:none;box-shadow: none;" data-dismiss="modal">&times;
                    </button>
                </div>

                <!-- Modal Corpo -->
                <div class="modal-body">
                    <input id ="conteudo" name="conteudo" aria-hidden="true" style="width:100%;border-color:#cccccc;outline:0;-webkit-box-shadow:none;box-shadow: none;" value="<?php echo $servico_manual->conteudo; ?>">   
                </div>
                <!-- Modal Rodape-->
                <div class="modal-footer">
                <button type="button" class="btn " style="background-color: #1d436f;color:white;outline:0;-webkit-box-shadow:none;box-shadow: none;" data-dismiss="modal">Fechar</button>

                <!-- Botão abaixo para salvar edições feitas no conteudo do Manual -->
                <button type="submit" class="btn" style="background-color: #1d436f;color: white;outline:0;-webkit-box-shadow:none;box-shadow: none;">Salvar Alterações</button>
                </div>

            </div>
        </div>
    </div>
</form>

Controller function:

public function salvar($id=null)
{
    $conteudo = $_POST["conteudo"];
    $this->Manualserv->conteudo = $conteudo;
    $this->Manualserv->atualizar_conteudo_manual($id);
    $data['page_title'] = 'Manual Servidor';
    $this->load->view('app/index', $data);
    redirect(base_url() . 'app/manual/manualservidores/', 'refresh');
}

Model:

public function atualizar_conteudo_manual($id_processo_manual) 
{
    $dadosmanual_update = array ("conteudo"=>$this->conteudo);
    return $this->db->update('tb_processos_manuais',$dadosmanual_update);
}
Author: novic, 2019-07-16

1 answers

In fact your problem is very basic, you are doing update without where, follow an example for query:

$this->db->set('field', 'field+1', FALSE);
$this->db->where('id', 2);
$this->db->update('mytable'); // gives UPDATE mytable SET field = field+1 WHERE id = 2

Without this where you will simply be updating everything.

If you want to know which exact query is being generated, you can use the following command:

print_r($this->db->last_query());    
 0
Author: Otto, 2019-08-01 19:37:47