Update with Codeigniter

Asked

Viewed 5,168 times

3

I am trying to update my database using codeigniter, however 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);
}

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

  • Gives a print_r($data), inside the controller, to see if the data are coming correctly.

  • if ($this->Texto_model->alterarText($data)) { print_r($data); die();

2 answers

2


In your HTML, add:

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

Inside your model, disable:

$this->db->set($data);

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 will give syntax sql error. Inside the array has the id property too, and you cannot 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... I hope I helped... Oss!

Browser other questions tagged

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