Update Codeigniter

Asked

Viewed 203 times

0

I’m trying to make a UPDATE in the database using Codeigniter, but the UPDATE is not being executed the way I’d like.

What happens, I have 5 manuals with different contents, and when I do the UPDATE this way it changes the contents of all manuals together, being that I want to change only the contents of a certain manual, I believe that I have to somehow use the identification of each one (id), but I have tried several ways and I could not reach 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>

Function of the Controller:

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);
}
  • Oops, it might be that the $id in the controller is going as null and updating everyone. In the html form, try to pass the id in the action, somehow send the url with the id: action="<? php echo base_url('app/Manual/save/48'); ?>"

  • It did not help, and even so if I passed the $id in the action it would always update the same no?

  • I think you are updating the table, but do a test. atualizar_conteudo_manual you can view your column information based on your id, use a var_dump or something, if you can see the values. Use the update on top of this data.

  • Using var_dump I can see the column information , however the id is coming as NULL, that’s the problem.

1 answer

0


Actually your problem is quite 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 simply you will be updating everything anyway.

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

print_r($this->db->last_query());    

Browser other questions tagged

You are not signed in. Login or sign up in order to post.