Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

html - HTTP Error 500 when adding products to cart (php ussing sessions)

I'm currently developing a simple shop project using PHP. After coding the entire shop (product list, detailed product list and some multilingual functions) I'm now adding a add to cart function, which lets users add the desired computer to a shopping cart.

Every time I try to add something to the cart, the page doesn't load correctly, causing an HTTP Error 500 - This page isn't working at the moment. Here's the cart.php code I have so far:

<?php
   Include ("funcs.php");
   session_start();   
   if ( isset($_GET['comprar']) ) {
      $_SESSION['carrinho'][$_GET['comprar']] += 1;
      header("Location: carrinho.php");
         exit;
   }
   print "<h1>".Traduz("Carrinho") ."</h1>
";
   $conn=LigaBD();
   print "<table>
";
   foreach( $_SESSION['carrinho'] as $sku => $quant ) {
     $campos = "Sku, Marca, Modelo, Preco as Unitario”;
     $query = "SELECT $campos FROM Produtos WHERE Sku='$sku'";
  // Database query

     $result = mysqli_query($conn, $query);
     if ( ! $result ) echo "erro sql
";
     $row = mysqli_fetch_assoc($result);

     print "  <tr>
";
     foreach ( $row as $val )
        print "<td>$val</td> 
";
     print "  </tr>";
   }
   print "</table>
";
?>

I'm still missing some functionalities like column titles, number of items added to the cart, total price, etc. but I guess that's easy to do after fixing this issue.

In order to get to cart.php, I have an hyperlink on my detailed product page, like this (function Traduz is used to translate in between Portuguese and English:

print "<a href='carrinho.php?comprar=$sku'>[".Traduz("+carrinho") . "]</a>";

The page is acessible via http://ave.dee.isep.ipp.pt/~1170747/cinem/lab11/list.php

function LigaBD() {
   $conn = new mysqli("localhost", "myuserishere", "mypwishere", "mydbishere");
   if ($conn -> connect_error) {
      die("connection failed: " . $conn->connect_error);
  }
  return $conn
}

EDIT1: Added the Include ("funcs.php") // Added exit; after header (Location) EDIT2: Added the database connect code

question from:https://stackoverflow.com/questions/65902106/http-error-500-when-adding-products-to-cart-php-ussing-sessions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...