Query with Arrays not working PHP SQL

Asked

Viewed 37 times

0

To edit a report, I need to take everything and turn it into an Array.

I cannot use UPDATE, because the user can add more lines, having q add.

Thus, I thought of first deleting the data that codeRelatory = '$codigoRelatorio' and then INSERT with the obtained data.

// Para pegar $codigoRelatorio //
if(isset($_GET['codigo'])){

    $codigoRelatorio = $_GET['codigo'];

    }






//////////////////////// PRESSIONAR O BOTÃO //////////////////////////
            if(isset($_POST['insertar']))

            {


            $items1 = ($_POST['codigoRelatorio']);

            $items2 = ($_POST['clienteRelatorio']);
            $items3 = ($_POST['nf']);
            $items4 = ($_POST['qtd']);
            $items5 = ($_POST['valorunitario']);
            $items6 = ($_POST['valorsubtotal']);

            $items7 = ($_POST['clientePrincipal']);
            $items8 = ($_POST['dataRelatorio']);

            $items9 = ($_POST['dataEntrega']);

            ///////////// SEPARAR VALORES DE ARRAYS, NESTE CASO SÃO 6 ARRAYS UM POR CADA INPUT (CODIGO, CLIENTE, NF, QUANTIDADE, PORPALETE, TOTAL)  ////////////////////)
            while(true) {

                //// RECUPERAR VALORES E JUNTÁ-LOS ////////
                $item1 = current($items1); //Código Relatorio
                $item2 = current($items2); //Cliente Relatorio
                $item3 = current($items3); //NF
                $item4 = current($items4); //Quantidade de Paletes
                $item5 = current($items5); //Valor por Palete
                $item6 = current($items6); //Valor Total
                $item9 = current($items9); //Data de Entrega
                $item7 = current($items7); //Cliente Principal
                $item8 = current($items8); //Data Relatorio

                ////// CONCATENAR PARA RESPECTIVAS VARIÁVEIS ///////////////////
                $codigo=(( $item1 !== false) ? $item1 : ", &nbsp");
                $cliente=(( $item2 !== false) ? $item2 : ",  ");
                $nf=(( $item3 !== false) ? $item3 : ",  ");
                $qntd=(( $item4 !== false) ? $item4 : ",  ");

                $porPalete=(( $item5 !== false) ? $item5 : ",  ");
                $total=(( $item6 !== false) ? $item6 : ",  ");
                $principal=(( $item7 !== false) ? $item7 : ", &nbsp");

                $dataRelatorio = (( $item8 !== false) ? $item8 : ", &nbsp");
                $dataEntrega = (( $item9 !== false) ? $item9 : ", &nbsp");

                //// CONCATENAR VALORES PARA FUTURA INSERÇÃO ////////
                $valores='('.$codigo.',"'.$cliente.'","'.$nf.'","'.$qntd.'","'.$porPalete.'","'.$total.'","'.$principal.'","'.$dataRelatorio.'","'.$dataEntrega.'"),';

                ////////  COMA É TERMINADO COM CADA LINHA, SUBTRAI COM FUNCÇÃO SUBSTR NA ÚLTIMA FILA /////////////////////
                $valoresQ= substr($valores, 0, -1);



                // QUERY PARA DELETAR DADOS ANTIGOS //
                $sql = "DELETE FROM relatorio WHERE codigoRelatorio = '$codigoRelatorio'";
                $qr = mysqli_query($connection, $sql) or die(mysqli_error());


                // QUERY PARA INSERIR NOVOS DADOS //
                $sql1 = "INSERT INTO relatorio (codigoRelatorio, clienteRelatorio, nf, quantidadePaletes, valorPorPalete, total, clientePrincipal, dataRelatorio, dataEntrega) 
                VALUES $valoresQ";
                $sql1Res=$connection->query($sql1) or mysqli_error();


                $idUltimo = mysqli_insert_id($connection);






                // Up! Next Value
                $item1 = next( $items1 );
                $item2 = next( $items2 );
                $item3 = next( $items3 );
                $item4 = next( $items4 );

                $item5 = next( $items5 );
                $item6 = next( $items6 );
                $item9 = next( $items9 );
                $item8 = next( $items8 );
                $item7 = next( $items7 );

                // Check terminator
                if($item1 === false && $item2 === false && $item3 === false && $item4 === false && $item5 === false && $item6 === false && $item7 === false && $item8 === false && $item9 === false) break;

            }



                if($sql){
                    echo "
        <script>window.open('verRelatorio.php?id=$idUltimo')</script>
        <meta http-equiv='refresh' content='0; url=relatorios.php' />
        <script type='text/javascript'>alert ('Dados foram Inseridos com Sucesso!!')</script>
    ";
                } else {
                    echo "
        <meta http-equiv='refresh' content='0; url=relatorios.php' />
        <script type='text/javascript'>alert ('Dados não foram Atualizados com Sucesso!!')</script>
    ";
                }

            }

?>

It works fine at first, but deletes old data, but when adding new data only adds the first line that the user has filled and does not all.

Anyway, I tried other ways, too, but I couldn’t. It seems to be all right, but it doesn’t add the data correctly.

Anyone can help????

  • How the variable is built: $valuesQ?

  • Avoid running query within a loop. Pass all data to an array and only run a query outside the loop.

  • @Guilhermepressuto, I will try to do this

  • Hello, $codeRelatory has different value than $code?

  • @Guilhermepressuto, thanks. That was it, put the DELETE FROM no isset insert. If you want, put as an answer there, I put as right!! VALEUUU

No answers

Browser other questions tagged

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