Update with Codeigniter

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 in which part of if was falling, and what appears is the success message, but nothing changes.

Author: RFL, 2015-09-14

2 answers

In your HTML, add:

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

Inside your model, disable:

$this->db->set($data);
 2
Author: Sr. André Baill, 2015-09-14 15:18:01

I believe that the "$this - >db - > set", should be field by field of the table in question, because if you drop all the array data data inside the set will give sql syntax error. Inside the array has the id property too, and you can't 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 that solves it... I hope I helped... Oss!

 1
Author: rpereira15, 2015-09-28 13:30:09