Loop error in stock control

Asked

Viewed 90 times

0

<?php
$sql = mysql_query ("SELECT * FROM cadastro ");
   while($resultado = mysql_fetch_array($sql)){
    $idrepete = $resultado['id'];
?>
#divId<echo idrepete; ?>{
width:100%;
}
<?php
   }
?>

This is the part that works, now is the part that I want to do:

<?php 


    if(isset($_POST['encerrar'])){

    $idpedidos1 = $pedido + 1;


        $contamais1 = mysql_query ("INSERT INTO idpedido (idpedido) VALUES ($idpedidos1) ");  // até aqui tudo beleza 



        $pedidosestoque = mysql_query("SELECT * FROM pedidos WHERE productid = $pedido");
        while($pedidosresultestoque = mysql_fetch_array($pedidosestoque)){
            $productidestoque = $pedidosresultestoque['productid'];
            $nomeestoque = $pedidosresultestoque['nome'];


    $pedidos2estoque = mysql_query("SELECT * FROM produtos WHERE id = $nome");
            while($pedidosresult2estoque = mysql_fetch_array($pedidos2estoque)){
                $nome2estoque = $pedidosresult2['nome'];
                $preco2estoque = $pedidosresult2['preco'];
                $estoqueid = $pedidosresult2estoque['id'];
                $estoquedisponivel2 = $pedidosresult2estoque['estoque'];


?>


<?php       
// Aqui é o que quero fazer, como no exemplo de css a #divId sempre recebe o id para ficar tipo #divId1, #divId2 etc, quero fazer o mesmo na variavel sql ali em baixo EXE
        $sql = mysql_query ("UPDATE produtos SET estoque = '$estoquedisponivelmenos' WHERE id=$estoqueid ");
    }
    }   
header("location:index.php");
}

Whenever the while assign a different value to my $sql so the stock balance error doesn’t happen. What happens is that only the last product posted that receives the stock balance I think it is because the last loop of $sql which counts in the case if the id ends in 3 only the product of id 3 will receive the stock balance.

  • 3

    I tried to improve but mainly the last paragraph is an incomprehensible text. I didn’t even get the title better because you can’t tell the real problem. See if you can make the problem easier to understand. Score. Write the text carefully. Think that those who can help you do not know what it is. Give all the details clearly. Don’t leave room for confusion. If you work hard on the text, who knows until you better understand the problem and can solve it yourself.

  • 1

1 answer

0


Start Guy you need to structure your algorithm more, program controllers so you have how to know what data and how they are behaving in each loop.

1 - I think you need a study in the library PDO which is much better and safer than mysql_fetch_array.

2 - if you are going to use it the way it is, make details in the code like: After executing the first select use the function, @mysql_num_rows to get an idea of the total data that is stored. Make Validations simple so you know what is happening in the algorithm, instead of while() listing everything just run in the total that exists and not run everything that exists in SELECT.

3 - Your ID will never change on account of you do not increment it, manually or using mysql_fetch_assoc, since there is no increment only the last value or the first one depends on how the DBMS will represent is that you will receive this UPDATE process that you want.

  • Thanks already managed using PDO vlw ai. mysql_num_rows really helped but PDO is more powerful vlw XD

Browser other questions tagged

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