Problem - Shopping cart increases products when refreshing page - PHP

0

From the series "I'm trying to make a complete shopping cart system" kk, I have one more problem. Just to emphasize that I am a beginner in programming and I am studying the maximum of functionalities and one of them is referring to shopping cart so I usually ask a lot of questions about it.

I will explain my problem but first I will say how the system is:

BRIEF EXPLANATION:

I have the shopping cart picking up the products from the database and inserting it into the front of the application. So there is also the feature add quantity of the same product, this is ok. It's that basic, if an array already exists with an item, it increments by 1 and so on. It's all right to remove. Now comes the bomb ^^

PROBLEM:

My problem lies in two things: When I add a product or remove it, it gives a refresh on the page and my idea is that it does not make this refresh, but do it in Run Time;

2nd Consider the example: Microphone Price: 30,00 Qty: 1;

Webcam Price: 22,00 Qty: 3; {< ====== This was the last product added, 3 quantity of the second product.}

The problem is that if I give a Refresh, it resends the request to add item and +1 the last product added.

Was I clear? I always think I explain my way and it's all bugged but it's just that I speak "I translate" kkkk

Well, these are my problems, and not to be alone in my words haha, I'll add the code of my cart, blz? Remembering that I know that maybe the error is in relation to SESSION's but I still do not understand the entire application and how to apply that it has so I count on a lot with your help. Here is the code:

<?php
/* Inicializa uma sessão */
//session_start();

/* Verifica se já existe uma sessão ativa */
if(!isset($_SESSION['itens'])){
    $_SESSION['itens'] = array();
}

/* Inicializa uma sessão */
if(isset($_GET['add']) && $_GET['add'] == 'carrinho'){
    $idProduto = $_GET['id'];
    if(!isset($_SESSION['itens'][$idProduto])){
        $_SESSION['itens'][$idProduto] = 1;
    }else{
        $_SESSION['itens'][$idProduto] += 1;
    }
}

/* Mostrar carrinho de compras */
echo "<div>";
if(count($_SESSION['itens']) == 0){ /* Se a sessão itens estiver vazia, retorna uma mensagem */
    echo "<div style='display : relative; float : right;'>Carrinho Vazio<br/><a href='index.php'>Adicionar Itens</a>";
}else{
    /* Cria conexão e retorna os na session itens os produtos e suas info como um array. */
    $conection = new PDO('mysql:host=localhost;dbname=lanchenet', 'admin', 'nuttertools'); 
    foreach($_SESSION['itens'] as $idProduto => $qtde){
        $query = $conection->prepare("SELECT * FROM produto WHERE id=?");
        $query->bindParam(1, $idProduto);
        $query->execute();
        $produtos = $query->fetchAll();
        $total = $qtde * $produtos[0]["valor"];
        echo
            $produtos[0]["nome"].'<br/>
            Preço: '.number_format($produtos[0]["valor"], 2, ",", ".").'<br/>
            Quantidade: '.$qtde.'<br/>
            Total: '.number_format($total, 2, ",", ".").'
            <a href="sistema.php?p=cardapio&remover=carrinho&id='.$idProduto.'">Remover</a>
            ';
        ;
    }
 echo "</div>";
 echo "</div>";
}

if(isset($_GET['remover']) && $_GET['remover'] == 'carrinho'){
    $idProduto = $_GET['id'];
    $_SESSION['itens'][$idProduto] -= 1;
    if ($_SESSION['itens'][$idProduto == 0]){
        unset($_SESSION['itens'][$idProduto]);
    }
    echo '<meta HTTP-EQUIV="REFRESH" CONTENT="0; URL=sistema.php?p=cardapio"/>'; 
}

echo "</div>";

? >

Belê? If you need some other piece of code just tell me what add. But that's it, I really want to understand this stop rsrsrs so any comments that contribute to a better understanding of this issue, will help me a lot .. Thank you all NATION! ^^

    
asked by anonymous 27.06.2018 / 04:50

0 answers