Page opening with error without recognizing variable $ _SESSION

0

Friends,

I'm getting a " Invalid argument supplied for foreach () ... and Undefinided Variable at line 43 " error when the page loads. These errors are being generated because the page is opening without logging in $ _ SESSION ["shopping_cart"] . The error only disappears from the page after I search for an item and select it, that is, the session ["shopping_cart"] only starts when I enter the code. I've tried a few tricks to make the [shopping_cart] session run as soon as the page is opened and without undefined variable, but I have not been successful so far. I believe the error is opening the session. Code below:

<?php
 if (!isset($_SESSION)){
isset($_SESSION["shopping_cart"]);
session_start();
?>
 <?php
 if(isset($_POST['hidden_name'])){

  $_SESSION["shopping_cart"][$_GET["idproduto"]] = array(
                'item_id' => $_GET["idproduto"],'item_name' => $_POST["hidden_name"],'item_serial' => @$_POST["hidden_serial"],'item_descricao' => @$_POST["hidden_descricao"],'item_qtd' => @$_POST["hidden_qtd"],'item_price' => $_POST["hidden_price"],'item_quantity' => $_POST["quantity"]);
return show_dynamic_list();
}

if (isset($_POST["action"]) && $_POST["action"] == "delete") {
unset($_SESSION["shopping_cart"][$_POST["idproduto"]]);
return show_dynamic_list();
}

function show_dynamic_list(){
?>
 <div id="main_box">

  <table class="head">
  <tr><th>MODELO</th><th>SERIAL</th><th>DESCRIÇÃO</th><th>ESTOQUE</th><th bgcolor="#D5FFFF">QTD</th><th>VALOR UNIT.</th><th>TOTAL</th></tr>
    <?php
    include('Connections/conexao.php'); 
$conexao = new mysqli("$hostname_conexao", "$username_conexao", "$password_conexao");
$conexao->query('SET NAMES utf8');
if (!$conexao) {
die("Database connection failed: " . mysqli_error($conexao));
}
$db_select = mysqli_select_db($conexao, $database_conexao);
if (!$db_select) {
die("Database selection failed: " . mysqli_error($conexao));
}
    $total = 0;
    foreach (@$_SESSION["shopping_cart"] as $keys => $values) {
        if(isset($_POST['receber_compra']) && $_POST['receber_compra'] == 'receber'){

$id = $values["item_id"];
$qtd = $values["item_quantity"];
$atual = $values["item_qtd"];
$totalrecebido = $qtd + $atual;
$envia = mysqli_query($conexao,"UPDATE produtos SET qtdatual = '$totalrecebido' WHERE idproduto='$id'");
echo '<script language="javascript">';
echo 'alert("Pedido recebido com sucesso!")';
echo '</script>';
}
        ?>
        <tr>
            <td><?php echo $values["item_name"]; ?></td>
            <td><?php echo $values["item_serial"]; ?></td>
            <td><?php echo $values["item_descricao"]; ?></td>
            <td><?php echo $values["item_qtd"]; ?></td>
            <td bgcolor="#D5FFFF"><?php echo $values["item_quantity"]; ?>   </td>
            <td>R$<?php echo $values["item_price"]; ?></td>
            <td><?php echo number_format($values["item_quantity"] * $values["item_price"], 2); ?></td>
            <td align="center">
                <form action="" method="post" class="delete_form">
                    <input type="hidden" name="idproduto" value="<?php echo $values["item_id"]; ?>"/>
                    <input type="hidden" name="action"  value="delete" />
                    <input type="submit" value="Remover" class="btn_delete" title="Deletar da lista"><!--coloquei o trecho de deletar na página buscadinamica2.php também, mas é aqui que o código funciona na página-->
                </form>
            </td>
        </tr>

        <?php

        $total = $total + ($values["item_quantity"] * $values["item_price"]);

    }
    ?>
    <tr>
        <td colspan="6" align="right">TOTAL DA ENTRADA:</td>
        <td colspan="1" align="center" bgcolor="#FFFF00">R$ <?php echo number_format($total, 2); ?></td>
        <td>
        <form method="post" enctype="multipart/form-data" class="form_receber" action="moventrada.php">
                  <input type="hidden" name="idproduto" value="<?php echo $id;?>" />
                  <input type="hidden" name="receber_compra" value="receber" />
                 <input type="submit" name="editar" value="Receber" class="btn_receber" title="Confirmar Recebimento" />
        </form>
        </td>
    </tr>
</table>

<?php
}
?>

  <?php include"header.php" ; ?>



    <div id="moviment">
    <form action="" class="compras" method="get" enctype="multipart/form-data">
           <span>DIGITE SUA PESQUISA:</span>
   <tr><input type="text" name="busca" id="busca" size="24" onFocus="this.style.backgroundColor='#FF9';" onBlur="this.style.backgroundColor='#ccc';" onKeyUp="buscar();" autocomplete="off" /></label></tr>
  </form>

        <div class="item_list">

        <div id="retornou"></div>

       </div><!--end "product_table"-->
       </div><!--end "item_list"-->

        <div class="returned">
            <?php show_dynamic_list(); ?>
        </div><!--fim "returned"-->           
   </div><!--fim "moviment"-->


    </div><!--fim "main_box"-->
    <script src="https://code.jquery.com/jquery-3.1.0.min.js"integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script>
<script type="text/javascript">
        $(function () {
        $('body').on('submit','#moviment form,.delete_form',function (event)            {
            event.preventDefault(); // Prevent the form from submitting via the browser
            var form = $(this);
            $.ajax({
                type:'post',
                url: form.attr('action'),
                data: form.serialize(),
                dataType:'html'
            }).done(function (data) {
                $('.returned').html(data);
            }).fail(function (data) {
                // error
            });
        });
    });

    </script>

 <?php include"footer.php"; ?> 
    
asked by anonymous 21.09.2016 / 19:32

0 answers