update array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tsalmeida
Forum Commoner
Posts: 38
Joined: Sat Jul 06, 2013 2:23 pm

update array

Post by tsalmeida »

hi

I am developing a simple shopping cart.

I add the products into your cart and is correctly showing the products and adding the quatidade accordingly.

My problem is the following:

my client requests that unless the amount of product q left the store stock and the value that was sold.
eg
product1 sold the two amounts by $ 30.00 for a total of $ 60.00 for 2 and product2 20.00 giving a total of $ 40.00

following this example, do the update in the inventory table properly and this saving amount, giving low properly.
however the value sold saving this wrong, this saving the value of the last product added to the cart, then the two products save the value of $ 40.00.

could help me with this problem?

code;

Code: Select all

<form action="?acao=up" method="post">
    <tfoot>
           <tr>
            <td colspan="5"><input type="submit" value="Atualizar Carrinho" /></td>
            <tr>
            <td colspan="5"><a href="vender.php">Continuar Comprando</a></td>
            
           
    </tfoot>
      
    <tbody>
               <?php
                     if(count($_SESSION['carrinho']) == 0){
                        echo '<tr><td colspan="5">Não há produto no carrinho</td></tr>';
                     }else{
                        require("conexao.php");
                                                               $total = 0;
                        foreach($_SESSION['carrinho'] as $idproduto => $qtd){
                              $sql   = "SELECT *  FROM estoque WHERE idproduto= '$idproduto'";
                              $qr    = mysql_query($sql) or die(mysql_error());
                              $ln    = mysql_fetch_assoc($qr);
                               
                              $idprodut = $ln['idproduto'];
                              $marca  = $ln['marca'];
                              $modelo  = $ln['modelo'];
                              $cor  = $ln['cor'];
                              $numero  = $ln['numero'];
                              $preco = number_format($ln['precovenda'], 2, ',', '.');
                              $sub   = number_format($ln['precovenda'] * $qtd, 2, ',', '.');
                               
                              $total += $ln['precovenda'] * $qtd;
                            
                           echo '<tr>
                     			<td>'.$idprodut.'</td>        
                                 <td>'.$marca.'</td>
                     			 <td>'.$modelo.'</td>
                  				 <td>'.$cor.'</td>
            					 <td>'.$numero.'</td>
                                 <td><input type="text" size="3" name="prod['.$idproduto.']" value="'.$qtd.'" /></td>
                                 <td>R$ '.$preco.'</td>
                                 <td>R$ '.$sub.'</td>
                                 <td><a href="?acao=del&id='.$idproduto.'">Remove</a></td>
                              </tr>';
                        }
                           $total = number_format($total, 2, ',', '.');
                           echo '<tr>
                                    <td colspan="4">Total</td>
                                    <td>R$ '.$total.'</td>
                              </tr>';
                     }
               ?>
    
     </tbody>
        </form>
</table>
<?php 

if(isset($_POST['enviar'])){
	$sqlinserevenda = mysql_query("INSERT into pedido(valortotal,data) VALUES ('$total',NOW())");
	
	$idvenda = mysql_insert_id();
	
	foreach($_SESSION['carrinho'] as $idproduto => $qtd){	
	$sub = $ln['precovenda'] * $qtd;
	$sqlinsereitens = mysql_query("INSERT into itenspedido(idpedido,idproduto,quantidade) VALUES('$idvenda','$idproduto','$qtd')");
	$sqlestoque = mysql_query("UPDATE estoque SET quantidade = quantidade - '$qtd' WHERE idproduto =  '$idproduto' ") or die(mysql_error());
	$sqlestoque1 = mysql_query("UPDATE estoque SET totalvenda = totalvenda + '$sub' WHERE idproduto =  '$idproduto' ") or die(mysql_error());
	}


	echo '<script> alert("Venda concluida!")</script>';
	}
?>
My problem was this line

Code: Select all

$sqlestoque1 = mysql_query("UPDATE estoque SET totalvenda = totalvenda + '$sub' WHERE idproduto =  '$idproduto' ") or die(mysql_error());	
Thanks
tsalmeida
Forum Commoner
Posts: 38
Joined: Sat Jul 06, 2013 2:23 pm

Re: update array

Post by tsalmeida »

no one can help me?
jdhmtl
Forum Newbie
Posts: 18
Joined: Tue Aug 12, 2014 7:33 am

Re: update array

Post by jdhmtl »

Code: Select all

UPDATE estoque SET totalvenda = totalvenda + '$sub' WHERE idproduto =  '$idproduto'
The query looks generally OK, although you probably don't want quotes around numeric values.

You mentioned which line the problem occurred on. What was the actual problem? What was the output of mysql_error()?
tsalmeida
Forum Commoner
Posts: 38
Joined: Sat Jul 06, 2013 2:23 pm

Re: update array

Post by tsalmeida »

the problem is that this command does not update inserting the correct values


look at my example

need to save the values ​​of goods sold.

put more than one product in the cart, not this saving the correct value of the first product added.

if you add a product with price of $ 1.00 and a second priced at $ 10.00, when running the command, it inserts the value of $ 10.00 for the two products sold


there s no mysql error

thanks
tsalmeida
Forum Commoner
Posts: 38
Joined: Sat Jul 06, 2013 2:23 pm

Re: update array

Post by tsalmeida »

damn

I found the error
my mistake to be precise lol

sorry for disturbing
Post Reply