title: How to insert data from shopping cart into the DB
body: <p>I made simple shopping cart using PHP for my website, so now I need to take data from that shopping cart and the form and send it to the DB. I tried writing the code myself but it doesn't work, when I press the submit button nothing happens. What could be the problem?</p>
  
  <p>This is the code I have:</p>
  
  <p>Index.php:</p>
  
  <pre><code>&lt;?php
  session_start();
  $connect = mysqli_connect("localhost", "root", "", "tut");
  ?&gt;
  
  &lt;!doctype html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;Top Food-Porucivanje za firme&lt;/title&gt;
  &lt;!-- Latest compiled and minified CSS --&gt;
  &lt;link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"&gt;
  &lt;!-- jQuery library --&gt;
  &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div class="container" style="width:60%;"&gt;
      &lt;h2 align="center"&gt;Top Food kolica&lt;/h2&gt;
      &lt;br /&gt;
      &lt;form&gt;
      &lt;input type="textbox" name="ime_firme" placeholder="Ime firme"&gt;
      &lt;input type="textbox" name="ime" placeholder="Ime"&gt;
      &lt;input type="textbox" name="prezime" placeholder="Prezime"&gt;
      &lt;/form&gt;
      &lt;br /&gt;
      &lt;br /&gt;
      &lt;?php
      $query = "SELECT `id`, `p_name`, `image`, `price` FROM `products` WHERE 1";
      $result = mysqli_query($connect, $query);
      if(mysqli_num_rows($result) &gt; 0)
      {
          while($row = mysqli_fetch_array($result))
          {
              ?&gt;
              &lt;div class="col-md-3"&gt;
              &lt;form method="post" action="shop.php?action=add&amp;id=&lt;?php echo $row["id"]; ?&gt;"&gt;
              &lt;div style="border: 1px solid #eaeaec; margin: -1px 19px 3px -1px; box-shadow: 0 1px 2px rgba(0,0,0,0.05); padding:10px;" align="center"&gt;
              &lt;img src="&lt;?php echo $row["image"]; ?&gt;" class="img-responsive"&gt;
              &lt;h5 class="text-info"&gt;&lt;?php echo $row["p_name"]; ?&gt;&lt;/h5&gt;
              &lt;h5 class="text-danger"&gt; &lt;?php echo $row["price"]; ?&gt; RSD&lt;/h5&gt;
              &lt;input type="text" name="quantity" class="form-control" value="1"&gt;
              &lt;input type="hidden" name="hidden_name" value="&lt;?php echo $row["p_name"]; ?&gt;"&gt;
              &lt;input type="hidden" name="hidden_price" value="&lt;?php echo $row["price"]; ?&gt;"&gt;
              &lt;input type="submit" name="add" style="margin-top:5px;" class="btn btn-default" value="Dodaj u kolica"&gt;
              &lt;/div&gt;
              &lt;/form&gt;
              &lt;/div&gt;
              &lt;?php
          }
      }
      ?&gt;
      &lt;div style="clear:both"&gt;&lt;/div&gt;
      &lt;h2&gt;Moja kolica&lt;/h2&gt;
      &lt;div class="table-responsive"&gt;
      &lt;table class="table table-bordered"&gt;
      &lt;tr&gt;
      &lt;th width="40%"&gt;Ime proizvoda&lt;/th&gt;
      &lt;th width="10%"&gt;Kolicina&lt;/th&gt;
      &lt;th width="20%"&gt;Cena&lt;/th&gt;
      &lt;th width="15%"&gt;Ukupno&lt;/th&gt;
      &lt;th width="5%"&gt;Komanda&lt;/th&gt;
      &lt;/tr&gt;
      &lt;?php
      if(!empty($_SESSION["cart"]))
      {
          $total = 0;
          foreach($_SESSION["cart"] as $keys =&gt; $values)
          {
              ?&gt;
              &lt;tr&gt;
              &lt;td name="ime"&gt;&lt;?php echo $values["item_name"]; ?&gt;&lt;/td&gt;
              &lt;td&gt;&lt;?php echo $values["item_quantity"] ?&gt; X&lt;/td&gt;
              &lt;td&gt; &lt;?php echo $values["product_price"]; ?&gt; RSD&lt;/td&gt;
              &lt;td&gt; &lt;?php echo number_format($values["item_quantity"] * $values["product_price"], 2); ?&gt; RSD&lt;/td&gt;
              &lt;td&gt;&lt;a href="shop.php?action=delete&amp;id=&lt;?php echo $values["product_id"]; ?&gt;"&gt;&lt;span class="text-danger"&gt;Ukloni&lt;/span&gt;&lt;/a&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;?php 
              $total = $total + ($values["item_quantity"] * $values["product_price"]);
          }
          ?&gt;
          &lt;tr&gt;
          &lt;td colspan="3" align="right"&gt;Ukupno&lt;/td&gt;
          &lt;td align="right"&gt; &lt;?php echo number_format($total, 2); ?&gt; RSD&lt;/td&gt;
          &lt;td&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;?php
      }
      ?&gt;
      &lt;/table&gt;
      &lt;input type="submit" value="Poruci"&gt;
      &lt;/div&gt;
      &lt;/div&gt;
   &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Shop.php:</p>
  
  <pre><code>&lt;?php
  session_start();
  $connect = mysqli_connect("localhost", "root", "", "tut");
  if(isset($_POST["add"]))
  {
  if(isset($_SESSION["cart"]))
  {
  $item_array_id = array_column($_SESSION["cart"], "product_id");
  if(!in_array($_GET["id"], $item_array_id))
  {
  $count = count($_SESSION["cart"]);
  $item_array = array(
  'product_id' =&gt; $_GET["id"],
  'item_name' =&gt; $_POST["hidden_name"],
  'product_price' =&gt; $_POST["hidden_price"],
  'item_quantity' =&gt; $_POST["quantity"]
  );
  $_SESSION["cart"][$count] = $item_array;
  echo '&lt;script&gt;window.location="index.php"&lt;/script&gt;';
  }
  else
  {
  echo '&lt;script&gt;alert("Proizvod je vec u kolicima")&lt;/script&gt;';
  echo '&lt;script&gt;window.location="index.php"&lt;/script&gt;';
  }
  }
  else
  {
  $item_array = array(
  'product_id' =&gt; $_GET["id"],
  'item_name' =&gt; $_POST["hidden_name"],
  'product_price' =&gt; $_POST["hidden_price"],
  'item_quantity' =&gt; $_POST["quantity"]
  );
  $_SESSION["cart"][0] = $item_array;
  }
  }
  if(isset($_GET["action"]))
  {
  if($_GET["action"] == "delete")
  {
  foreach($_SESSION["cart"] as $keys =&gt; $values)
  {
  if($values["product_id"] == $_GET["id"])
  {
  unset($_SESSION["cart"][$keys]);
  echo '&lt;script&gt;alert("Proizvod je uklonjen")&lt;/script&gt;';
  echo '&lt;script&gt;window.location="index.php"&lt;/script&gt;';
  }
  }
  }
  }
  ?&gt;
  </code></pre>
  
  <p>My attempt at sending data into the database:</p>
  
  <pre><code>&lt;?php
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "tut";
  
  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn-&gt;connect_error) {
      die("Connection failed: " . $conn-&gt;connect_error);
  } 
  
  $sql = "INSERT INTO tut (p_name, image, price)
  VALUES ('{$mysqli-&gt;real_escape_string($_POST['ime'])}')";
  
  if ($conn-&gt;query($sql) === TRUE) {
      echo "New record created successfully";
  } else {
      echo "Error: " . $sql . "&lt;br&gt;" . $conn-&gt;error;
  }
  
  $conn-&gt;close();
  ?&gt;
  </code></pre>