Loop Error in Inventory Control

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 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 I run while assign a different value to my $sql so that the inventory balance error does not occur. What happens is that only the last product posted that receives the stock balance I think that is because the last loop of the $sql that counts in the case if the id ends in 3 only the product of id 3 will receive the stock balance.

    
asked by anonymous 12.02.2015 / 21:37

1 answer

0

Startup You need to further structure your algorithm, 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 PDO library that is much better and safer than mysql_fetch_array .

2 - If you're going to use it the way it is, put details into 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 simple Validations for you to know what is happening in the algorithm, instead of while() listing everything just run in the total that exists and not execute everything that exists in the SELECT.

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

    
13.02.2015 / 01:00