Records are duplicating when entering in the database

Asked

Viewed 1,593 times

0

I’m doing an API and I’m going through a problem. When executing the script that calls the page via Curl, it does the insertion of a register in the database, but when it comes to registering only one record, it registers 2 equal.

Curl.php

    <?php
    $ch = curl_init();

    $data = array('acao'=>'1', 'dados'=>array('nome'=>'Alisson Acioli', 'cpf'=>'xxxx', 'nascimento'=>date('Y-m-d'), 'email'=>'[email protected]', 'endereco'=>'xxx', 'bairro'=>'itaquera', 'estado'=>'SP', 'cidade'=>'São Paulo', 'referencia'=>'', 'nomemae'=>'Cecilia', 'cep'=>'08215255', 'endproprio'=>'1', 'nacionalidade'=>'Brasileiro', 'sexo'=>'Masculino', 'nomepai'=>'José Antonio', 'grau'=>'Ensino médio', 'estadocivil'=>'Casado', 'pessoafisica'=>'sim', 'login'=>'alisson', 'senha'=>'123456', 'natureza'=>'1'));

    curl_setopt($ch, CURLOPT_URL, "http://localhost:8080/xxxx/api.php");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

    $output = curl_exec($ch);

echo $output;

curl_exec($ch);


?>

api.php

<?php
protected function CadastrarUsuario(){

        $dados = $this->dados["dados"];

        //echo json_encode($dados);
        $condition = '';

        foreach($dados as $colunm=>$row){

            $condition .= '"'.$row.'"'.", ";
        }

            $condition = utf8_decode(substr($condition, 0, -2));
            $insert = mysql_query("INSERT INTO cadastros VALUES (NULL, $condition)");

            if($insert){

                echo 'Registro inserido com sucesso!';
            }else{
                echo 'Erro ao realizar registro: '.mysql_error();
            }

    }
?>
  • Put the code where CadastrarUsuario() is called.

  • 1

    public Function Executeaction(){ switch($this->acao){ case 1: $this->Sign up user(); break; case 2: echo 'Is the secondary action; break; } }

  • In the webserver log there are one or two Http requests for each Curl script run?

  • There is only one..

1 answer

2

You are running curl_exec($ch) twice; I think the last function should be curl_close($ch);

<?php
    $ch = curl_init();

    $data = array('acao'=>'1', 'dados'=>array('nome'=>'Alisson Acioli', 'cpf'=>'xxxx', 'nascimento'=>date('Y-m-d'), 'email'=>'[email protected]', 'endereco'=>'xxx', 'bairro'=>'itaquera', 'estado'=>'SP', 'cidade'=>'São Paulo', 'referencia'=>'', 'nomemae'=>'Cecilia', 'cep'=>'08215255', 'endproprio'=>'1', 'nacionalidade'=>'Brasileiro', 'sexo'=>'Masculino', 'nomepai'=>'José Antonio', 'grau'=>'Ensino médio', 'estadocivil'=>'Casado', 'pessoafisica'=>'sim', 'login'=>'alisson', 'senha'=>'123456', 'natureza'=>'1'));

    curl_setopt($ch, CURLOPT_URL, "http://localhost:8080/xxxx/api.php");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

    $output = curl_exec($ch);

echo $output;

curl_close($ch); // <== aqui


?>
  • I believe this is it!

  • I fixed... I put the "login" column as UNIC in Mysql, so it does not duplicate if you have the same login. Thank you!

  • in any case remove the duplicity of the curl_exec function, as it is running twice the same request without need...

Browser other questions tagged

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