Avoid registering duplicates in database with Codeigniter

1

How about, I tell you, I have a form and register everything well. What I need now is that once a code has been registered, it will no longer be possible to register it again.

This is the form:

<?php echo form_open("PaquetesController/adds"); ?>

<div class="form-group has-feedback has-feedback-left">
                <label class="col-sm-2 control-label"
                      for="descripcion" >Giro de su empresa</label>
                <div class="col-sm-10">
                    <input type="name" class="form-control"
                        name="giro_empresa" placeholder="Venta de consumibles y refacciones"/>
                        <i class="form-control-feedback glyphicon glyphicon-home"></i>

                </div>
              </div>



               <div class="form-group has-feedback has-feedback-left">
                <label class="col-sm-2 control-label"
                      for="descripcion" >codigo</label>
                <div class="col-sm-10">
                    <input type="name" class="form-control"
                        name="key_suscriptor" placeholder=" hsd7shdya7y$%d"/>
                        <i class="form-control-feedback glyphicon glyphicon-home"></i>

                </div>
              </div>

              <div class="col-sm-6">
              <button class="btn bg-olive margin" name="submit"> Agregar nuevo</i></button>
              </div>

                     <?php echo form_close(); ?>

Here is the controller:

    public function adds(){

$this->load->model('PaqueteModel');

    $this->PaqueteModel->insertPruebas();
    }

Here is the Model:

public function insertPruebas(){

    $data = array(


    'giro_empresa'=>$this->input->post('giro_empresa'),
    'key_suscriptor'=>$this->input->post('key_suscriptor'),


     );

    $this->db->insert(TABLE_PRODUCTOS,$data);


    }

I researched and found something, I adapted it to my code but it does not register anything, I do not know if you can help me understand it or how to adapt it in the correct way:

    function value_exists($key)
{
    $this->db->where('TABLE_PRODUCTOS', $key);
    $query = $this->db->get(TABLE_PRODUCTOS);
    if ($query->num_rows() > 0){
        return true;
    }
    else{
        return false;
    }
}

Thank you in advance.

    
asked by x4mp73r 07.04.2016 в 05:43
source

1 answer

1

I have solved it, staying like this:

 $query = $this->db->get_where(TABLE_PRODUCTOS, array('key_suscriptor' => $data['key_suscriptor']));
            if ($query->num_rows() > 0) {
                echo'<script>alert("Error. !alguien ya ah registrado ese código!")</script>';
            } else {
     echo'<script>alert("!Gracias por resgistrarte!")/script>'             
$this->db->insert(TABLE_PRODUCTOS,$data);


                }
    
answered by 07.04.2016 / 06:38
source