Verify that information already exists in the database

0

Good people try to help me here on this

Part html

<form method="post" id="formulario" action="dados.php">  
   <p><label>Nome</label><input type="text" class="nome" name="nome" size="60" /></p>  
            <div id="telefone">  
        <p><label>Telefone</label><input type="text" class="fone" name="fone[]" size="15" /><span class="adicionar">Adicionar telefone</span></p>  
   </div>  
        <p><input type="submit" value="Enviar" id="btEnviar" name="btEnviar" />  
 </form> 

Part php

<?php  

$ligacao = mysqli_connect("localhost", "root", "", "teste");
                        if (mysqli_connect_errno()) {
                            echo "Erro na liga??o MySQL: " . mysqli_connect_error();
                        }


$nome=$_POST["nome"];





foreach($_POST['fone'] as $fone)  
{


    $inf = "select nome, tel from teste where nome = '".$nome."'";
    $res=mysqli_query($ligacao,$inf);

    while( ($registo = mysqli_fetch_assoc($res)) !=null)
   {

                $infoNome=$registo["nome"];
                $infoTel=$registo["tel"];
                echo $infoNome;
                echo $infoTel;
   }


   if ($infoTel==$fone){

   }
   else{
     $sql = "insert into teste (nome, tel) values ('$nome' ,'$fone')";
     $resultado = mysqli_query($ligacao,$sql);
}  
}
?>

What I want to do is that in the php part when analyzing all the numbers of mobile phone that the person introduces it will compare and if it already exists in the database does not let add again

To be easier to visualize if I entered these numbers I wanted to only enter 345, 234 and 123 once, but that is not what is happening and I can not understand why

    
asked by anonymous 01.05.2015 / 00:32

1 answer

1

Well, I'm not sure how mysqli works, so I'm going to display a practical method I'd use by using UNIQUE bruno said there, I'll do in PDO , but you adapt to your own !

$fone = $_POST["fone"]; //recebe o post
$fone = array_unique($fone); //elimina array duplicadas
foreach($fone as $f){ //salva tudo no BD
        $this->databaseConnection();
        $prepara = $this->db_connection->prepare('INSERT INTO seubanco (telefone) VALUES (:telefone)');
        $prepara->bindValue(':telefone', $f, PDO::PARAM_STR);
        $prepara->execute();
}

With the past explanation, I could only think about it!

I hope that helps you.

    
01.05.2015 / 05:42