Update with Codeigniter

3

I'm trying to update my database using codeigniter, but the update is not running.

HTML:

<?php echo form_open('Texto/alterarTexto', 'class="form-horizontal"'); ?>
  <fieldset>
    <legend>Novo texto</legend>
    <div class="form-group">
      <label class="col-md-1 control-label" for="titulo">Titulo</label>
      <div class="col-md-6">
        <input id="titulo" name="titulo" type="text" placeholder="" class="form-control input-md" value="<?php echo $dados[0]->titulo; ?>" required disabled>
      </div>
    </div>
    <div class="form-group">
      <label class="col-md-1 control-label" for="data">Data</label>
      <div class="col-md-2">
      <input id="data" name="data" type="hidden" placeholder="" class="form-control input-md" required value="<?php echo $dados[0]->id; ?>">
        <input id="data" name="data" type="text" placeholder="" class="form-control input-md" required value="<?php echo $dados[0]->data; ?>">
      </div>
    </div>
    <div class="form-group">
      <label class="col-md-1 control-label" for="inputDefault">Texto <br /> <small>Evite grandes textos</small></label>
      <div class="col-md-9">
        <textarea rows="10" id="area1" cols="120" name="texto"><?php echo $dados[0]->texto; ?></textarea>
      </div>
    </div>
    <div class="form-group">
      <label class="col-md-1 control-label" for="salvar"></label>
      <div class="col-md-5">
        <button id="salvar" class="btn btn-primary btn-lg" type="submit">Salvar</button>
      </div>
    </div>
  </fieldset>
  

Controller:

public function alterarTexto() {

    $data = array(  'id' => $this->input->post('id'),
                    'data' => $this->input->post('data'),
                    'texto' => $this->input->post('texto')
                    );
    if ($this->Texto_model->alterarTexto($data)) {
        echo "Sucesso!";
    } else {
        echo "Erro!";
    }


}

Model:

    function alterarTexto($data) {
    $this->db->where('id', $data['id']);
    $this->db->set($data);
    return $this->db->update('texto', $data);
}

In the controller I put echo just to know what part of if was falling, and what appears is the success message, but nothing changes.

    
asked by anonymous 14.09.2015 / 16:50

2 answers

2

In your HTML, add:

<input type="hidden" value="<?php echo dados[0]->id; ?>" name="id">

Within your model, disable:

$this->db->set($data);
    
14.09.2015 / 17:18
1

I believe that the "$ this-> db-> set" should be field by field of the table in question, because if you drop the entire $ data array within the set it will give syntax error sql. Inside the array has the id property too, and you can not update it ...

function alterarTexto($data) {
    $this->db->where('id', $data['id']);
    $this->db->set('Titulo', $data['Titulo']);
    $this->db->set('Data', $data['Data']);
    $this->db->set('Texto', $data['Texto']);
    $this->db->update('texto');
    if($this->db->trans_status() === true){
        $this->db->trans_commit();
        return true;
    }else{
        $this->db->trans_rollback();
        return false;
    }
}

I think it solves this way ... Hope this helps... Oss!

    
26.09.2015 / 00:12