0

I have a question about a shopping cart i am building right now, i get a 500 error when i submit the form to my cart, so the system don't catch my item at the cart at all. Any idea how to fix this? Here is the code, thanks in advance your help is appreciated.

index.php file

<?php   
session_start();  
$connect = mysqli_connect("localhost", "root", "12345", "shopping");  
if(isset($_POST["add_to_cart"]))  
{  
if(isset($_SESSION["shopping_cart"]))  
{  
$item_array_id = array_column($_SESSION["shopping_cart"], "item_id");  
if(!in_array($_GET["id"], $item_array_id))  
{  
$count = count($_SESSION["shopping_cart"]);  
$item_array = array(  
'item_id'               =>     $_GET["id"],  
'item_name'               =>     $_POST["hidden_name"],  
'item_price'          =>     $_POST["hidden_price"],  
'item_quantity'          =>     $_POST["quantity"]  
);  
$_SESSION["shopping_cart"][$count] = $item_array;  
echo '<script>window.location="cart.php"</script>';
}  
else  
{   
echo '<script>window.location="cart.php"</script>';  
}  
}  
else  
{  
$item_array = array(  
'item_id'               =>     $_GET["id"],  
'item_name'               =>     $_POST["hidden_name"],  
'item_price'          =>     $_POST["hidden_price"],  
'item_quantity'          =>     $_POST["quantity"]  
);  
$_SESSION["shopping_cart"][0] = $item_array;  
}  
}  
if(isset($_GET["action"]))  
{  
if($_GET["action"] == "delete")  
{  
foreach($_SESSION["shopping_cart"] as $keys => $values)  
{  
if($values["item_id"] == $_GET["id"])  
{  
unset($_SESSION["shopping_cart"][$keys]);   
echo '<script>window.location="cart.php"</script>';  
}  
}  
}  
}  
?>

Now this is my form post action:

action="index.php?action=add&id=<?php echo $row["id"]; ?>"

card.php file

<?php   
 session_start();  
 $connect = mysqli_connect("localhost", "root", "12345", "shopping"); 
 if(isset($_POST["add_to_cart"]))  
 {  
      if(isset($_SESSION["shopping_cart"]))  
      {  
           $item_array_id = array_column($_SESSION["shopping_cart"], "item_id");  
           if(!in_array($_GET["id"], $item_array_id))  
           {  
                $count = count($_SESSION["shopping_cart"]);  
                $item_array = array(  
                     'item_id'               =>     $_GET["id"],  
                     'item_name'               =>     $_POST["hidden_name"],  
                     'item_price'          =>     $_POST["hidden_price"],  
                     'item_quantity'          =>     $_POST["quantity"]  
                );  
                $_SESSION["shopping_cart"][$count] = $item_array;  
           }  
           else  
           {    
                echo '<script>window.location="cart.php"</script>';  
           }  
      }  
      else  
      {  
           $item_array = array(  
                'item_id'               =>     $_GET["id"],  
                'item_name'               =>     $_POST["hidden_name"],  
                'item_price'          =>     $_POST["hidden_price"],  
                'item_quantity'          =>     $_POST["quantity"]  
           );  
           $_SESSION["shopping_cart"][0] = $item_array;  
      }  
 }  
 if(isset($_GET["action"]))  
 {  
      if($_GET["action"] == "delete")  
      {  
           foreach($_SESSION["shopping_cart"] as $keys => $values)  
           {  
                if($values["item_id"] == $_GET["id"])  
                {  
                     unset($_SESSION["shopping_cart"][$keys]);  
                     echo '<script>window.location="cart.php"</script>';  
                }  
           }  
      }  
 }  
 ?>

Now when i submit i get the 500 error on the page and the browser shows me this link: http://localhost/shops/index.php?action=add&id=1 on my domain. Any idea how to fix this issue? I got no clue at all. :(

  • Should start by reporting errors http://php.net/manual/en/function.error-reporting.php. it will tell you more on what's happening. http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – Louis Loudog Trottier May 11 '17 at 04:23
  • Enable error reporting and check your logs. A 500 error can mean a lot of things, and guessing isn't very efficient – Qirel May 11 '17 at 04:23
  • i got this error on it: Fatal error: Call to undefined function array_column() in /shops/index.php on line 13 – Pedro Carvalho May 11 '17 at 05:00
  • Which version of PHP are you running? – Qirel May 11 '17 at 05:04
  • I am running a PHP Version 5.4.16 on my server – Pedro Carvalho May 11 '17 at 05:22
  • In my localhost with external connection to my server it works just fine but online at my domain name it does not i get this error, i also cant seem to find what i am missing here. Any help is much appreciated. @Qirel – Pedro Carvalho May 11 '17 at 05:28
  • [As per the documentation](http://php.net/manual/en/function.array-column.php) you can see that `array_column()` was introduced in PHP 5.5.0. If you're running any lower than that, the function simply won't exist. So either upgrade the version (recommended anyways), or write the code without that function. – Qirel May 11 '17 at 05:40
  • 1
    Damm that is correct. Need to update my php version at the server, well i hope i didnt made you lose any precious time mate, at least i know my code is working perfectly, thanks again. @Qirel – Pedro Carvalho May 11 '17 at 05:51
  • No worries, happy to have helped ;-) *Cheers* – Qirel May 11 '17 at 06:03

0 Answers0