Check if user already exists codeigniter

Asked

Viewed 82 times

0

Hi, I’m having a little trouble checking if a dependent’s name has already been registered. I have a form where the person can add up to 5 names of dependents and after that is generated a tab with the names.

Form field, have a button to add more fields.

    <input type="text" name="nome_dep[]" id="nome_dep" placeholder="Nome do Dependente" class="form-control>

Page of the Controller

$nome = $this->input->post('nome_dep');
$nbanco = "SELECT `nome_dependente` FROM `beneficios_dependentes` WHERE `nome_dependente` IN ? ";

    $nome_existe = $this->db->query($nbanco,array($nome))->result_array();

    if (empty($nome_existe))
    {
        echo "Não tem nome no banco";
    }
    else
    {
        echo "Tem nome no banco";
    }

My difficulty is in checking if the name of the dependent has already been registered, for example: he registered John - OK, but the other day he registered John and Mary is falling that already has the name registered in the bank. I need to do an Insert if it has not been registered and an update if it is already registered

  • But There are namesakes, the correct would not be to add the nome_dependente and the cpf_dependente, so that you can compare the CPF’s and not the names? The end there are many "Luiz Inácio Lula", but not all steal countries. some just want to program.

  • kkkk, I will change the name of the kkkk profile, so dependents are linked with an associate, will I get something in that direction?

  • I think it would be less "correct", suppose an associate has twin children with the same name... he could not put the two children because they have the same names? Why the SELECT nome_dependente FROM beneficios_dependentes WHERE nome_dependente = $nome AND associado_id = $id_do_associado wouldn’t do? How are your tables?

  • Table dependent benefits has: id_dependent benefits, dependent name_name, dependent associate_dependents (FK), the problem is that the name comes in an array name="name_dep[]", I need to separate each one and check in the table. The issue of twins I believe will not have problem, but you gave an idea to add the ID

  • I never used codeignite, but if the problem is how to do the select for all the names of a array, you use some repeat loop like the foreach(). I will publish the answer as I believe it would be.

2 answers

0

From what I understand, your problem is in running the search by the amount of benefits that arrive by post

$nomes = $this->input->post('nome_dep');
  foreach ($nome as $nome) {

    $nbanco = "SELECT `nome_dependente` FROM `beneficios_dependentes` WHERE `nome_dependente` = $nome";

    $nome_existe = $this->db->query($nbanco,array($nome))->result_array();

    if (empty($nome_existe))
    {
      echo "Não tem nome no banco";
    }
    else
    {
      echo "Tem nome no banco";
    }
  }

Try it on and give me a return

  • I’ve arranged it’s worked out, thank you so much for your help

  • If you can just dial in the right answer

0


foreach ($nomes as $nome) 
    {
        $this->db->select('nome_dependente');
        $this->db->where('nome_dependente', $nome);
        $query = $this->db->get('beneficios_dependentes');
        $resultado = $query->result();
        if (empty($resultado))
        {
          $insere = $this->beneficios_model->insere($id, $dados, $nome);
        }
        else
        {
          echo "Tem nome no banco";
        }
    }

I did it like this, I’m fixing it, but it worked

Browser other questions tagged

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