No matter the products for the PHP cart || MYSQL

Asked

Viewed 69 times

0

I’m making an online store and I’m having trouble making the cart, when I click on the icon to add to cart, it doesn’t matter and the cart always appears empty.

inserir a descrição da imagem aqui

And the result:

inserir a descrição da imagem aqui

This is the button code

    include ("db.php");
        $molcomp_query="SELECT * FROM stock_comp WHERE (diametroaco BETWEEN '$d1' and '$d2') AND (comprimentototal BETWEEN '$comp1' AND '$comp2') AND (diametroexterior BETWEEN '$de1' AND '$de2') $ordenar_por";
        
         $run_query = mysqli_query($con,$molcomp_query);
        if (mysqli_num_rows($run_query) > 0){
           foreach($run_query as $row){
            
            //while($row = mysqli_fetch_array($run_query)){
                $id_mol_comp=$row['id_mol_comp'];
                $referencia=$row['referencia'];
                $diametroaco=$row['diametroaco'];
                $comprimentototal=$row['comprimentototal'];
                $diametroexterior=$row['diametroexterior'];
                $passo=$row['passo'];
                $preco=$row['preco'];
               echo
                "<div class='card mx-auto w-100'>
                    <div class='card-body'>
                            <div class='row'>
                            <div class='col-md-1' style='text-align: center'>$referencia</div>
                            <div class='col-md-2' style='text-align: center'>$diametroaco</div>
                            <div class='col-md-2' style='text-align: center'>$comprimentototal</div>
                            <div class='col-md-2' style='text-align: center'>$diametroexterior</div>
                            <div class='col-md-1' style='text-align: center'>$passo</div>
                            <div class='col-md-1' style='text-align: center'>$preco €</div>
<div class='col-md-2' style='text-align: center'><i style='font-size:30px' class='fa'>&#xf1c1;</i> &nbsp &nbsp &nbsp 
<a class='fa fa-cart-plus' style='font-size:30px' href='carrinho.php?add=carrinho&id= '".$row['id_mol_comp']."'></a> </div>
                        </div>
                    </div>
                </div>
                ";
              
           }
        }

This is the cart code:

<!DOCTYPE html>
<?php session_start();
    ?>
<html lang="en">

  <head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="author" content="">

   
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
      
    <!-- Bootstrap core CSS -->
   <link rel="stylesheet" type="text/css" media="screen" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="//stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="//stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.bundle.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="main.js"></script>
        
      
    <style>
h3 {
color:#1751A3;
}
</style>
      
      
      
    </head>
    
    <body>
        <div class="container-fluid">
    
<?php
if (!isset($_session['itens'])){
    $_SESSION['itens']=array();          
}
           if(isset($GET['add'])&& $GET['add']=="carrinho") {
               $idproduto = $GET['id'];
               if(!isset($_session['itens'][$idproduto])){
                  $_session['itens'][$idproduto]=1;  
               }else{
                  $_session['itens'][$idproduto]+=1; 
               }
           }
          
            if (count($_SESSION['itens'])==0){
                echo'Carrinho vazio <br><a href="mol_compressao.php">Adicionar Itens</a>';
            }else{
                echo"
                 <h3>Carrinho de Compras</h3>
 <div align='center'>
      <div class='card mx-auto w-75'>
        <div class='container-fluid''>
            <div class='row align-items-end' style='height:40px;'>
                <div class='col-md-2' style='text-align: center'>REFERÊNCIA</div>
                <div class='col-md-3' style='text-align: center'>QUANTIDADE</div>
                <div class='col-md-2' style='text-align: center'>PREÇO UNITÁRIO</div>
                <div class='col-md-2' style='text-align: center'>PREÇO TOTAL</div>
            </div>
        </div> 
          <hr>
                    <div class='card-body' >
                        <div class='row align-items-end' style='height:0px;'>
                            <div class='col-md-10' style='text-align: center'>Total:</div> 
                            <div class='col-md-2' style='text-align: center'>ef</div>
                        </div>
                    </div>
        </div>
</div> 
";
                include ("db.php");
                foreach($_session['itens'] as $produtos => $quantidade){
                    $select=$con->prepare("select * from stock_comp where id=?");
                    $select->bindparam(1,$idproduto);
                    $select->execute();
                    $fetch=$select->fetchAll();
                    echo"
                    $precototal=$preco*$quantidade
                        <div class='card-body'>
                        <div class='row align-items-center' style='height:0px;'>
                            <div class='col-md-2' style='text-align: center'>$Referencia</div>
                            <div class='col-md-3' style='text-align: center'>$quantidade</div>
                            <div class='col-md-3' style='text-align: center'></div>
                            <div class='col-md-3' style='text-align: center'><input type='text' name='de2' size='1' ><i style='font-size:24px' class='fa'>&#xf147;</i><i style='font-size:24px' class='fa'>&#xf196;</i></div>
                            <div class='col-md-2' style='text-align: center'>$preco</div>
                            <div class='col-md-2' style='text-align: center'>$precototal</div>
                        </div>
                    </div> 
          <hr>";
                       
                }
            }
            
?>
        
  </div>

  </body>

</html>

No error, but it also shows nothing.

1 answer

1

Here’s a problem, you’re putting the variable $row in quotes without need:

<a class='fa fa-cart-plus' style='font-size:30px' href='carrinho.php?add=carrinho&id=".$row['id_mol_comp']."'>

To session_start() should come first of all in the code:

<?php session_start();?>
<!DOCTYPE html>

Browser other questions tagged

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