System does not add and edit, just view

Asked

Viewed 31 times

0

I’m having trouble with a trademark registration system.

The system is viewing the marks correctly, but does not edit or include new marks, just reload the page.

models>marcas_model.php

<?php
class Marcas_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }


    function get($table,$fields,$where='',$perpage=0,$start=0,$one=false,$array='array'){

        $this->db->select($fields);
        $this->db->from($table);
        $this->db->order_by('idMarcas','desc');
        $this->db->limit($perpage,$start);
        if($where){
            $this->db->where($where);
        }

        $query = $this->db->get();

        $result =  !$one  ? $query->result() : $query->row();
        return $result;
    }

    function getById($id){
        $this->db->where('idMarcas',$id);
        $this->db->limit(1);
        return $this->db->get('marcas')->row();
    }

    function add($table,$data){
        $this->db->insert($table, $data);         
        if ($this->db->affected_rows() == '1')
        {
            return TRUE;
        }

        return FALSE;       
    }

    function edit($table,$data,$fieldID,$ID){
        $this->db->where($fieldID,$ID);
        $this->db->update($table, $data);

        if ($this->db->affected_rows() >= 0)
        {
            return TRUE;
        }

        return FALSE;       
    }

    function delete($table,$fieldID,$ID){
        $this->db->where($fieldID,$ID);
        $this->db->delete($table);
        if ($this->db->affected_rows() == '1')
        {
            return TRUE;
        }

        return FALSE;        
    }   

    function count($table){
        return $this->db->count_all($table);
    }}

**controllers>marcas.php**
<?php

class Marcas extends CI_Controller {

    function __construct() {
        parent::__construct();
        if ((!$this->session->userdata('session_id')) || (!$this->session->userdata('logado'))) {
            redirect('mapos/login');
        }

        $this->load->helper(array('form', 'codegen_helper'));
        $this->load->model('marcas_model', '', TRUE);
        //$this->data['menuMarcas'] = 'Marcas';
    }

    function index(){
        $this->gerenciar();
    }

    function gerenciar(){

        if(!$this->permission->checkPermission($this->session->userdata('permissao'),'aServico')){
           $this->session->set_flashdata('error','Você não tem permissão para visualizar serviços.');
           redirect(base_url());
        }


        $this->load->library('pagination');


        $config['base_url'] = base_url().'index.php/marcas/gerenciar/';
        $config['total_rows'] = $this->marcas_model->count('marcas');
        $config['per_page'] = 10;
        $config['next_link'] = 'Próxima';
        $config['prev_link'] = 'Anterior';
        $config['full_tag_open'] = '<div class="pagination alternate"><ul>';
        $config['full_tag_close'] = '</ul></div>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li><a style="color: #2D335B"><b>';
        $config['cur_tag_close'] = '</b></a></li>';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['first_link'] = 'Primeira';
        $config['last_link'] = 'Última';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';

        $this->pagination->initialize($config);     

        $this->data['results'] = $this->marcas_model->get('marcas','idMarcas,nome,descricao,preco','',$config['per_page'],$this->uri->segment(3));

        $this->data['view'] = 'marcas/marcas';
        $this->load->view('tema/topo',$this->data);



    }

    function adicionar() {
        if(!$this->permission->checkPermission($this->session->userdata('permissao'),'aServico')){
           $this->session->set_flashdata('error','Você não tem permissão para adicionar serviços.');
           redirect(base_url());
        }

        $this->load->library('form_validation');
        $this->data['custom_error'] = '';

        if ($this->form_validation->run('marcas') == false) {
            $this->data['custom_error'] = (validation_errors() ? '<div class="form_error">' . validation_errors() . '</div>' : false);
        } else {
            $preco = $this->input->post('preco');
            $preco = str_replace(",","", $preco);

            $data = array(
                'nome' => set_value('nome'),
                'descricao' => set_value('descricao'),
                'preco' => $preco,
            );

            if ($this->marcas_model->add('marcas', $data) == TRUE) {
                $this->session->set_flashdata('success', 'Serviço adicionado com sucesso!');
                redirect(base_url() . 'index.php/marcas/adicionar/');
            } else {
                $this->data['custom_error'] = '<div class="form_error"><p>Ocorreu um erro.</p></div>';
            }
        }
        $this->data['view'] = 'marcas/adicionarMarca';
        $this->load->view('tema/topo', $this->data);

    }

    function editar() {
        if(!$this->permission->checkPermission($this->session->userdata('permissao'),'eServico')){
           $this->session->set_flashdata('error','Você não tem permissão para editar serviços.');
           redirect(base_url());
        }
        $this->load->library('form_validation');
        $this->data['custom_error'] = '';

        if ($this->form_validation->run('marcas') == false) {
            $this->data['custom_error'] = (validation_errors() ? '<div class="form_error">' . validation_errors() .'</div>' : false);
        } else {
            $preco = $this->input->post('preco');
            $preco = str_replace(",","", $preco);
            $data = array(
                'nome' => $this->input->post('nome'),
                'descricao' => $this->input->post('descricao'),
                'preco' => $preco,
            );

            if ($this->marcas_model->edit('marcas', $data, 'idMarcas', $this->input->post('idMarcas')) == TRUE) {
                $this->session->set_flashdata('success', 'Serviço editado com sucesso!');
                redirect(base_url() . 'index.php/marcas/editar/'.$this->input->post('idMarcas'));
            } else {
                $this->data['custom_error'] = '<div class="form_error"><p>Ocorreu um errro.</p></div>';
            }
        }

        $this->data['result'] = $this->marcas_model->getById($this->uri->segment(3));

        $this->data['view'] = 'marcas/editarMarca';
        $this->load->view('tema/topo', $this->data);

    }

    function excluir(){

        if(!$this->permission->checkPermission($this->session->userdata('permissao'),'eServico')){
           $this->session->set_flashdata('error','Você não tem permissão para excluir serviços.');
           redirect(base_url());
        }


        $id =  $this->input->post('id');
        if ($id == null){

            $this->session->set_flashdata('error','Erro ao tentar excluir serviço.');            
            redirect(base_url().'index.php/marcas/gerenciar/');
        }

        /*$this->db->where('marcas_id', $id);
        $this->db->delete('marcas_os');*/

        $this->marcas_model->delete('marcas','idMarcas',$id);             


        $this->session->set_flashdata('success','Serviço excluido com sucesso!');            
        redirect(base_url().'index.php/marcas/gerenciar/');
    }
}

views>tags>tags.php

   <?php if($this->permission->checkPermission($this->session->userdata('permissao'),'aServico')){ ?>
        <a href="<?php echo base_url()?>index.php/marcas/adicionar" class="btn btn-success"><i class="icon-plus icon-white"></i> Adicionar Marca</a>
    <?php } ?>

    <?php 

    if(!$results){?>

        <div class="widget-box">
         <div class="widget-title">
            <span class="icon">
                <i class="icon-wrench"></i>
             </span>
            <h5>Marcas</h5>

         </div>

    <div class="widget-content nopadding">


    <table class="table table-bordered ">
        <thead>
            <tr style="backgroud-color: #2D335B">
                <th>#</th>
                <th>Nome</th>
                <th>Preço</th>
                <th>Descrição</th>
                <th></th>
            </tr>
        </thead>
        <tbody>

            <tr>
                <td colspan="5">Nenhuma Marca Cadastrado</td>
            </tr>
        </tbody>
    </table>
    </div>
    </div>



    <?php } 
    else{ ?>

    <div class="widget-box">
         <div class="widget-title">
            <span class="icon">
                <i class="icon-wrench"></i>
             </span>
            <h5>Marcas</h5>

         </div>

    <div class="widget-content nopadding">


    <table class="table table-bordered ">
        <thead>
            <tr style="backgroud-color: #2D335B">
                <th>#</th>
                <th>Nome</th>
                <th>Descrição</th>
                <th>Preço</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($results as $r) {
                echo '<tr>';
                echo '<td>'.$r->idMarcas.'</td>';
                echo '<td>'.$r->nome.'</td>';
                echo '<td>'.number_format($r->preco,2,',','.').'</td>';
                echo '<td>'.$r->descricao.'</td>';
                echo '<td>';
                if($this->permission->checkPermission($this->session->userdata('permissao'),'aServico')){
                    echo '<a style="margin-right: 1%" href="'.base_url().'index.php/marcas/editar/'.$r->idMarcas.'" class="btn btn-info tip-top" title="Editar Serviço"><i class="icon-pencil icon-white"></i></a>'; 
                }
                if($this->permission->checkPermission($this->session->userdata('permissao'),'aServico')){
                    echo '<a href="#modal-excluir" role="button" data-toggle="modal" marca="'.$r->idMarcas.'" class="btn btn-danger tip-top" title="Excluir Serviço"><i class="icon-remove icon-white"></i></a>  '; 
                }    


                echo '</td>';
                echo '</tr>';
            }?>
            <tr>

            </tr>
        </tbody>
    </table>
    </div>
    </div>





    <?php echo $this->pagination->create_links();} ?>


    <!-- Modal -->
    <div id="modal-excluir" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <form action="<?php echo base_url() ?>index.php/marcas/excluir" method="post" >
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h5 id="myModalLabel">Excluir Serviço</h5>
      </div>
      <div class="modal-body">
        <input type="hidden" id="idMarca" name="id" value="" />
        <h5 style="text-align: center">Deseja realmente excluir este serviço?</h5>
      </div>
      <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Cancelar</button>
        <button class="btn btn-danger">Excluir</button>
      </div>
      </form>
    </div>


    <script type="text/javascript">
    $(document).ready(function(){


       $(document).on('click', 'a', function(event) {

            var marca = $(this).attr('marca');
            $('#idMarca').val(marca);

        });

    });

    </script>

append.php

<div class="row-fluid" style="margin-top:0">
    <div class="span12">
        <div class="widget-box">
            <div class="widget-title">
                <span class="icon">
                    <i class="icon-align-justify"></i>
                </span>
                <h5>Cadastro de Marca</h5>
            </div>
            <div class="widget-content nopadding">
                <?php echo $custom_error; ?>
                <form action="<?php echo current_url();?>" id="formMarca" method="post" class="form-horizontal" >
                    <div class="control-group">
                        <label for="nome" class="control-label">Nome<span class="required">*</span></label>
                        <div class="controls">
                            <input id="nome" type="text" name="nome" value="<?php echo set_value('nome'); ?>"  />
                        </div>
                    </div>
                    <div class="control-group">
                        <label for="preco" class="control-label"><span class="required">Preço*</span></label>
                        <div class="controls">
                            <input id="preco" class="money" type="text" name="preco" value="<?php echo set_value('preco'); ?>"  />
                        </div>
                    </div>
                    <div class="control-group">
                        <label for="descricao" class="control-label">Descrição</label>
                        <div class="controls">
                            <input id="descricao" type="text" name="descricao" value="<?php echo set_value('descricao'); ?>"  />
                        </div>
                    </div>

                    <div class="form-actions">
                        <div class="span12">
                            <div class="span6 offset3">
                                <button type="submit" class="btn btn-success"><i class="icon-plus icon-white"></i> Adicionar</button>
                                <a href="<?php echo base_url() ?>index.php/marcas" id="btnAdicionar" class="btn"><i class="icon-arrow-left"></i> Voltar</a>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script src="<?php echo base_url()?>js/jquery.validate.js"></script>
<script src="<?php echo base_url();?>js/maskmoney.js"></script>
<script type="text/javascript">
      $(document).ready(function(){
          $(".money").maskMoney();
           $('#formMarca').validate({
            rules :{
                  nome:{ required: true},
                  preco:{ required: true}
            },
            messages:{
                  nome :{ required: 'Campo Requerido.'},
                  preco :{ required: 'Campo Requerido.'}
            },

            errorClass: "help-inline",
            errorElement: "span",
            highlight:function(element, errorClass, validClass) {
                $(element).parents('.control-group').addClass('error');
            },
            unhighlight: function(element, errorClass, validClass) {
                $(element).parents('.control-group').removeClass('error');
                $(element).parents('.control-group').addClass('success');
            }
           }); 
      });
</script>     

edit.php

<div class="row-fluid" style="margin-top:0">
                              <div class="span12">
                                    <div class="widget-box">
                                          <div class="widget-title">
                                                <span class="icon">
                                                      <i class="icon-align-justify"></i>
                                                </span>
                                                <h5>Editar Serviço</h5>
                                          </div>
                                          <div class="widget-content nopadding">
                                                <?php echo $custom_error; ?>
                                                <form action="<?php echo current_url(); ?>" id="formMarca" method="post" class="form-horizontal" >
                                                    <?php echo form_hidden('idMarcas',$result->idMarcas) ?>
                                                    <div class="control-group">
                                                            <label for="nome" class="control-label">Nome<span class="required">*</span></label>
                                                            <div class="controls">
                                                                  <input id="nome" type="text" name="nome" value="<?php echo $result->nome ?>"  />
                                                            </div>
                                                      </div>
                                                      <div class="control-group">
                                                            <label for="preco" class="control-label"><span class="required">Preço*</span></label>
                                                            <div class="controls">
                                                                <input id="preco" class="money" type="text" name="preco" value="<?php echo $result->preco ?>"  />
                                                            </div>
                                                      </div>
                                                      <div class="control-group">
                                                            <label for="descricao" class="control-label">Descrição</label>
                                                            <div class="controls">
                                                                  <input id="descricao" type="text" name="descricao" value="<?php echo $result->descricao ?>"  />
                                                            </div>
                                                      </div>

                                                      <div class="form-actions">
                                                      <div class="span12">
                                                            <div class="span6 offset3">
                                                            <button type="submit" class="btn btn-primary"><i class="icon-ok icon-white"></i> Alterar</button>
                                                            <a href="<?php echo base_url()?>index.php/marcas" id="btnAdicionar" class="btn"><i class="icon-arrow-left"></i> Voltar</a>
                                                            </div>
                                                      </div>
                                                      </div>
                                                </form>
                                          </div>
                                    </div>
                              </div>
</div>

<script src="<?php echo base_url()?>js/jquery.validate.js"></script>
<script src="<?php echo base_url();?>js/maskmoney.js"></script>
<script type="text/javascript">
      $(document).ready(function(){
          $(".money").maskMoney();
           $('#formMarca').validate({
            rules :{
                  nome:{ required: true},
                  preco:{ required: true}
            },
            messages:{
                  nome :{ required: 'Campo Requerido.'},
                  preco :{ required: 'Campo Requerido.'}
            },

            errorClass: "help-inline",
            errorElement: "span",
            highlight:function(element, errorClass, validClass) {
                $(element).parents('.control-group').addClass('error');
            },
            unhighlight: function(element, errorClass, validClass) {
                $(element).parents('.control-group').removeClass('error');
                $(element).parents('.control-group').addClass('success');
            }
           });
      });
</script>
  • You even debug the Submit of these forms to see if it’s coming in the application and how it’s coming in?

  • @Bruno Rigolon Includes this line in the controller and the typed values have been returned correctly //start if(isset($_POST['name'])){echo "<br><br>CHECKING OK: - ". $_POST['name'];}Else {echo "<br><br>ERROR";} if(isset($_POST['preco'])){echo "<br>CHECKING OK: - ". $_POST['preco'];}Else {echo "ERROR";} if(isset($_POST['Description'])){echo "<br>CHECKING OK: - ". $_POST['Description'];}Else {echo "ERROR";} //end

2 answers

0

Try to make this change, see if you will enter the records.

// Arquivo
# models>marcas_model.php

// Altere isto (function adicionar)
$data = array(
    'nome' => set_value('nome'),
    'descricao' => set_value('descricao'),
    'preco' => $preco,
);

// Para isso
$data = array(
    'nome' => $this->input->post('nome'),
    'descricao' => this->input->post('descricao'),
    'preco' => $preco,
);

0

@Bruno Rigolon

Thank you for the answer, but the problem was not at this point. However analyzing the code below I discovered the problem.

if ($this->form_validation->run('marcas') == false)

I forgot to include the table "marks" in form_validation.

Thank you for your attention!

Browser other questions tagged

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