0
Hello, I develop a program for the store I work and when we order to open a coupon, it checks to see if it is opening an existing coupon (with the variable $_GET['cupom']) or if it is a new coupon. It worked very well, even existed a if with too much redirecting in this code before the last update, had never given error of Multiple Redirects but now it is giving I do not know why. 
When the browser displays the error screen with the ERR_TOO_MANY_REDIRECTS, the displayed url is http://localhost/administrar/pedidos_venda.php?cupom=1&aberto=1 as if the else if(!$existeCodigoCupom) was executed. But if true, the program ignored the if's previous.
I learned everything I know by myself, through documentations and forums. It may have errors, feel free to indicate, so I learn a little more.
Follow the php code:
if(!isset($_GET['cupom']))
{
    $existeCodigoCupom = DBRead("vendapedidos", "ORDER BY id DESC LIMIT 1");
    $codigoCupom = DBRead("vendapedidos" , "WHERE status = '-1' AND aberto = '0' ORDER BY id ASC LIMIT 1");
    if($codigoCupom)
    {
        if($codigoCupom[0]['aberto'] == 0)
        {
            $codigo = $codigoCupom[0]['id'];
            $dados = array
            (
                'dataHoraCriacao' => date('Y-m-d H:i:s'),
                'dataHoraAtualizacao' => date('Y-m-d H:i:s'),
                'valores' => "0,,0",
                'status' => -1
            );
            $registraCupom = DBUpDate("vendapedidos", $dados, "id = '{$codigo}'");
            if($registraCupom)
            {
                header('Location: /administrar/pedidos_venda.php?cupom='.$codigo);
                exit();
            }
            else
            {
                echo "Não foi possível abrir o cupom!";
                exit();
            }
        }
    }
    else if($existeCodigoCupom)
    {
        $codigo = $codigoCupom[0]['id'];
        $codigo++;
        $dados = array
        (
            'dataHoraCriacao' => date('Y-m-d H:i:s'),
            'dataHoraAtualizacao' => date('Y-m-d H:i:s'),
            'valores' => "0,,0",
            'status' => -1
        );
        $registraCupom = DBCreate("vendapedidos", $dados);
        if($registraCupom)
        {
            header('Location: /administrar/pedidos_venda.php?cupom='.$codigo);
            exit();
        }
        else
        {
            echo "Não foi possível abrir o cupom!";
            exit();
        }
    }
    else if(!$existeCodigoCupom)
    {
        $codigo = 1;
        $dados = array
        (
            'dataHoraCriacao' => date('Y-m-d H:i:s'),
            'dataHoraAtualizacao' => date('Y-m-d H:i:s'),
            'valores' => "0,,0",
            'status' => -1
        );
        $registraCupom = DBCreate("vendapedidos", $dados);
        if($registraCupom)
        {
            header('Location: /administrar/pedidos_venda.php?cupom='.$codigo);
            exit();
        }
        else
        {
            echo "Não foi possível abrir o cupom!";
            exit();
        }
    }
}
Of a
print_rin$existeCodigoCupomand$codigoCupom, show me the result of this!– Wagner Fillio
The function
DBReaddoes reading in the dandos database, is returning array with the results, I believe that the values will not influence here, if you want to send in another comment, then I will try to explain them. The variable$existeCodigoCupomchecks if any value is returned, so there is already a coupon saved in the bank and$codigoCupomdoes a bank search to see if there is coupon with thestatus = '-1'which are coupons that were opened but did not have an assigned customer, so reuse the coupon.– Túlio Moreira
To clarify, if I return and try to open the page again, the page works normally
– Túlio Moreira
put in the comment @Túliomoreira, the var_dump($existeCodigoCupom), we need to know what comes in this variable to be able to help you better. Hug
– user148170
printed value for
$existeCodigoCupom:Array ( [0] => Array ( [id] => 30 [cobranca] => [dataHoraCriacao] => 2019-08-16 17:42:47 [dataHoraAtualizacao] => 2019-08-16 17:42:47 [idCliente] => 0 [cliente] => [endereco] => [bairro] => [cidade] => [estado] => [telefone] => [produtos] => [valores] => 0,,0 [status] => -1 [aberto] => 0 ) )– Túlio Moreira