PHP: Shopping Cart - Everything works except EDIT

Hi everyone,

I’m building a shopping cart and everything works except the option to edit the number of products. Here is some of the code:


<form action="carrinho.php?action=edit" method="POST">

.
.
.

switch($_GET['action']) {
if (isset($_GET['action']))
        {
            if (isset($_GET['id']))
            {
                $id = $_GET['id'];
case "edit":
                    
                        if (isset($_POST['quantidade']))
                        {
                            if (is_array($_POST['quantidade']))
                            {
                                foreach($_POST['quantidade'] as $id_produto => $quantidade)
                                {
                                    if (is_numeric($quantidade))
                                    {
                                        $alterar_quantidade = mysql_query("UPDATE carrinho SET quantidade='$quantidade' WHERE id_produto='$id_produto', sessao='".session_id()."'") or die('Erro: ' . mysql_erro());
                                    }
                                }
                            }
                        }
                        break;

.
.
.


<?php 
            include ("conexao.php");
                            
            $listagem = mysql_query("SELECT * FROM carrinho WHERE sessao='".session_id()."' ORDER BY nome ASC") or die('Erro: ' . mysql_error());

            $contador_listagem = mysql_num_rows($listagem);
            if ($contador_listagem != "")
            {    
                $preco_total = 0;
                
                while($row_listagem = mysql_fetch_assoc($listagem))
                {
                        $preco_total += ($row_listagem['preco']*$row_listagem['quantidade']);
            ?>        
            <tr>
            <td><?php echo $row_listagem['nome']; ?></td>
            <td><?php echo number_format($row_listagem['preco'],3,",","") . "&#8364;"; ?></td>
            <td><input type="text" size="3" name="quantidade[<?php echo $row_listagem['id_produto']; ?>]" value="<?php echo $row_listagem['quantidade']; ?>" />
            </td>
            <td><?php echo number_format($row_listagem['preco']*$row_listagem['quantidade'],3,",","") . "&#8364;"; ?></td>
            <td><a href="carrinho.php?action=del&id=<?php echo $row_listagem['id_produto']; ?>">Remover</a></td>
            </tr>
            <?php    
                }
            }
            ?>
            <tr>
                <td colspan="4">Total</td>
                <td><?php echo number_format($preco_total,3,",",""); ?></td>
        </table>
        <input type="submit" value="Alterar"/>
        </form>

When I edit the value on the page, it does nothing. I think the problem is somewhere on sending the value to my CASE.

Thanks in advance.

^ I would say so. Your code is confusing. I mean, I don’t know if you can have if within a switch… You can have if within case, but I don’t think within switch.

Also, MySQL update “where … and …”

Hi,

I was tired and I probably past it wrong. I don’t have if inside switch, here ir how I’m starting my code:


if (isset($_GET['action']))
        {
            if (isset($_GET['id']))
            {
                $id = $_GET['id'];
                
                switch($_GET['action'])

The problem keeps happening, really don’t know what I’m doing wrong.

It’s working :slight_smile:
Problem solved.

My last post helped me out. I had switch inside my if (isset($_GET[‘id’])).

Thank you.