Need help with Php shop insert id

I need help very badly. This is for a school project.

I am a beginner to php and i’m trying to creat a simple webshop. I am able to insert all order details into my database execpt for the product id (vare_id). I have had this problem for many weeks with no avail. In my example below you can see the insert. I have to manyally insert the product id, which is 2.

$INSERT2 = “INSERT Into bestilling_has_vare (bestilling_bestilling_id, vare_vare_id, vare_varenavn, kvantum) values(’$last_id’, ‘2’, ‘{$values[“varenavn”]}’, ‘{$values[“kvantum”]}’)”;

When i try to use the product id then it doesn’t do anything. I can echo different values like the product name, but when i do so with the product id then there is nothing there. Here is some more of the code.

if(isset($_POST["add_to_cart"]))  

{  

     if(isset($_SESSION["shopping_cart"]))  

     {  

          $item_array_id = array_column($_SESSION["shopping_cart"], "vare_id");  

          if(!in_array($_GET["vare_id"], $item_array_id))  

          {  

               $count = count($_SESSION["shopping_cart"]);  

               $item_array = array(  

                    'vare_id'               =>     $_POST["vare_id"],  

                    'varenavn'               =>     $_POST["hidden_name"],  

                    'varepris'          =>     $_POST["hidden_price"],  

                    'kvantum'          =>     $_POST["kvantum"]  

               );  

               $_SESSION["shopping_cart"][$count] = $item_array;  

          }  

          else  

          {  

               echo '<script>alert("Item Already Added")</script>';  

               echo '<script>window.location="varer.php"</script>';  

          }  

     }  

     else  

     {  

          $item_array = array(  

               'vare_id'               =>     $_POST["vare_id"],  

               'varenavn'               =>     $_POST["hidden_name"],  

               'varepris'          =>     $_POST["hidden_price"],  

               'kvantum'          =>     $_POST["kvantum"]  

          );  

          $_SESSION["shopping_cart"][0] = $item_array;  

     }  

}  

if(isset($_GET["action"]))  

{  

     if($_GET["action"] == "delete")  

     {  

          foreach($_SESSION["shopping_cart"] as $keys => $values)  

          {  

               if($values["vare_id"] == $_GET["vare_id"])  

               {  

                    unset($_SESSION["shopping_cart"][$keys]);  

                    echo '<script>alert("Vare tatt vekk")</script>';  

                    echo '<script>window.location="varer.php"</script>';  

               }  

          }  

     }  

}  

$gateadresse = $_POST['gateadresse'];

$gatenr = $_POST['gatenr'];  

$mobil = $_POST['mobil'];  

$postnummer = $_POST['postnummer'];

$INSERT = "INSERT Into bestilling (gateadresse, users_id, gatenr, mobil, postnummer) values('$gateadresse', '{$_SESSION["id"]}', '$gatenr', '$mobil', '$postnummer')"; 

if ($conn->query($INSERT) === TRUE) {

    $last_id = $conn->insert_id;

}

mysqli_query($conn, $INSERT); 

foreach($_SESSION["shopping_cart"] as $keys => $values)  

{     

$INSERT2 = "INSERT Into bestilling_has_vare (bestilling_bestilling_id, vare_vare_id, vare_varenavn, kvantum) values('$last_id', '2', '{$values["varenavn"]}', '{$values["kvantum"]}')";

mysqli_query($conn, $INSERT2); 

}

$conn->close();

?>

I chose the values from inside the body to the website. This is the php part in the body.
foreach($_SESSION["shopping_cart"] as $keys => $values)  

                               {  

                          ?>  

                          <tr>  

                               <td><?php echo $values["varenavn"]; ?></td>  

                               <td><?php echo $values["kvantum"]; ?></td>  

                               <td>$ <?php echo $values["varepris"]; ?></td>

                               <td>$ <?php echo number_format($values["kvantum"] * $values["varepris"], 2); ?></td>  

                               <td><a href="handlevogn.php?action=delete&id=<?php echo $values["vare_id"]; ?>"><span class="text-danger">Slett</span></a></td>  

                          </tr>  

                          <?php  

                                    $total = $total + ($values["kvantum"] * $values["varepris"]);  

                               }  

                          ?>  

                          <tr>  

                               <td colspan="3" align="right">Total</td>  

                               <td align="right">$ <?php echo number_format($total, 2); ?></td>  

                               <td></td>  

                          </tr>  

                          <?php  

                          }  

                          ?>

Does anyone know what’s wrong? I havn’t found any solution to this.

You would need to post the full code that reproduces the problem, since we don’t have any idea where the value is supposed to be coming from.

The answer is clear, after looking at what the code is doing. The product id is in -

If you tried using that and it didn’t insert a value, then there’s probably something wrong with the add to cart form.

What does using var_dump($_SESSION["shopping_cart"]); show for the contents of the cart?

Sponsor our Newsletter | Privacy Policy | Terms of Service