title: How to fix the Add function?
body: <p>I'm doing a php based project.And I want to add data to the database.I did it using php, PDO. And I created the code with OOC. But There's a if else part in main HTML page. In that part the else part is running. But I didn't even click the insert button. When the page is loading the else part is running. I watched a video and this code is working perfectly fine. Here's the code..</p>
  
  <p>Insert.php - Getters, Setters, SQL query and etc.</p>
  
  <pre><code>    class Stock
      {
          protected $ItemNo;
          protected $ItemName;
          protected $brand;
          protected $qty;
          protected $Description;
          protected $ItemDate;
          protected $SupplierName;
          protected $SupplierMail;
          private $tableName = 'StockDetails';
          private $dbconn;
  
          function setItemNo($ItemNo) { $this-&gt;ItemNo = $ItemNo; }
          function getItemNo() { return $this-&gt;ItemNo; }
          function setItemName($ItemName) { $this-&gt;ItemName = $ItemName; }
          function getItemName() { return $this-&gt;ItemName; }
          function setBrand($brand) { $this-&gt;brand = $brand; }
          function getBrand() { return $this-&gt;brand; }
          function setQty($qty) { $this-&gt;qty = $qty; }
          function getQty() { return $this-&gt;qty; }
          function setDescription($Description) { $this-&gt;Description = $Description; }
          function getDescription() { return $this-&gt;Description; }
          function setItemDate($ItemDate) { $this-&gt;ItemDate = $ItemDate; }
          function getItemDate() { return $this-&gt;ItemDate; }
          function setSupplierName($SupplierName) { $this-&gt;SupplierName = $SupplierName; }
          function getSupplierName() { return $this-&gt;SupplierName; }
          function setSupplierMail($SupplierMail) { $this-&gt;SupplierMail = $SupplierMail; }
          function getSupplierMail() { return $this-&gt;SupplierMail; }
  
          public function __construct()
          {
              require_once ('Database.php');
              $db = new Database();
              $this-&gt;dbconn = $db-&gt;connect();
  
          }
          public function insert(){
  
              $sql = "INSERT INTO $this-&gt;tableName VALUES (:ItemNo, :ItemName, :brand, :qty, :Description, :ItemDate, :SupplierName, :SupplierMail)";
              $stmt = $this-&gt;dbconn-&gt;prepare($sql);
  
              $stmt-&gt;bindParam(':ItemNo', $this-&gt;ItemNo);
              $stmt-&gt;bindParam(':ItemName', $this-&gt;ItemName);
              $stmt-&gt;bindParam(':brand', $this-&gt;brand);
              $stmt-&gt;bindParam(':qty', $this-&gt;qty);
              $stmt-&gt;bindParam(':Description', $this-&gt;Description);
              $stmt-&gt;bindParam(':ItemDate', $this-&gt;ItemDate);
              $stmt-&gt;bindParam(':SupplierName', $this-&gt;SupplierName);
              $stmt-&gt;bindParam(':SupplierMail', $this-&gt;SupplierMail);
  
              if($stmt-&gt;execute()){
  
                  return true;
  
              }else{
  
                  return false;
              }}}  
  ?&gt;
  </code></pre>
  
  <p>Action.php - Form action on AddItem.php</p>
  
  <pre><code>    require_once ('Insert.php');
  
      class action{
  
          function __construct(){
  
              switch ($_POST['submit']) {
                  case 'insert':
                      $obInsert = new Stock;
  
                      $obInsert-&gt;setItemNo($_POST['ItemNo']);
                      $obInsert-&gt;setItemName($_POST['ItemName']);
                      $obInsert-&gt;setBrand($_POST['brand']);
                      $obInsert-&gt;setQty($_POST['qty']);
                      $obInsert-&gt;setDescription($_POST['Description']);
                      $obInsert-&gt;setItemDate(date('Y-m-d H:i:s'));
                      $obInsert-&gt;setSupplierName($_POST['SupplierName']);
                      $obInsert-&gt;setSupplierMail($_POST['SupplierMail']);
  
                      if($obInsert-&gt;insert()) {
  
                          header('location: AddItem.php?insert=1');
  
                      } else{
  
                          header('location: AddItem.php?insert=0');
                      }
  
                      break;      
                  default:
                      header('location: AddItem.php');
                      break;
              }
          }
      }
  
      if(isset($_POST['submit'])){
  
          $object = new action;
      }
  </code></pre>
  
  <p>AddItem.php</p>
  
  <p>To get the brand and ItemName from the Database. The brand changes according to ItemName.</p>
  
  <pre><code>&lt;?php
  require_once ('GetBrand.php');
  $ItemName = LoadItemName();
  
  ?&gt;
  
     $(document).ready(function(){
          $("#ItemName").change(function(){
            var aid = $("#ItemName").val();
            $.ajax({
               url: 'GetBrand.php',
               method: 'post',
               data: 'aid=' + aid
             }).done(function(brand){
               console.log(brand);
               brand = JSON.parse(brand);
               $('#brand').empty();
               brand.forEach(function(bnamee){
               $('#brand').append('&lt;option&gt;' + bnamee.brand + '&lt;/option&gt;')
               })
             })
           })
          })
  </code></pre>
  
  <p>The Form </p>
  
  <pre><code>&lt;form name="form0" method="post" action="Action.php"&gt;
      &lt;div class="form-group"&gt;
           &lt;label for="text"&gt;Item No&lt;/label&gt;
           &lt;input type="text" name="ItemNo" id="ItemNo" placeholder="Item No" required&gt;
       &lt;/div&gt;
       &lt;form name="form1" action="AddItem.php" method="post"&gt;
            &lt;div class="form-group"&gt;
                &lt;label for="text"&gt;Item Name&lt;/label&gt;
                &lt;table&gt;
                   &lt;tr&gt;
                      &lt;td&gt;&lt;select name="ItemName" id="ItemName" required&gt;
                      &lt;option value="" disabled="" selected&gt;Select Name&lt;/option&gt;
                      &lt;?php foreach($ItemName as $iname)
                          echo "&lt;option id='".$iname['ItemNo']."' value='".$iname['ItemNo']."'&gt;".$iname['ItemName']."&lt;/option&gt;";
                      ?&gt;
                    &lt;/select&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;label for="text"&gt;Add Item Name : &lt;/label&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;input type="text" name="name" id="name"&gt; &lt;/td&gt;
                    &lt;td&gt;&lt;button&gt;Add&lt;/button&gt;&lt;/td&gt;
                  &lt;/tr&gt;
               &lt;/table&gt;
           &lt;/div&gt;
           &lt;div class="form-group"&gt;
               &lt;label for="text"&gt;Brand Name&lt;/label&gt;
               &lt;table&gt;
                  &lt;tr&gt;
                     &lt;td&gt;&lt;select name="brand" id="brand" required&gt;
                     &lt;option value=""&gt;Select Brand&lt;/option&gt;
                     &lt;/select&gt;&lt;/td&gt;
                     &lt;td&lt;label for="text"&gt;Add Brand : &lt;/label&gt;&lt;/td&gt;
                     &lt;td&gt;&lt;input type="text" name="Bname" id="Bname"  class="form-control" &gt; &lt;/td&gt;
                     &lt;td&gt;&lt;button&gt;Add&lt;/button&gt;&lt;/td&gt;
                 &lt;/tr&gt;
             &lt;/table&gt;
          &lt;/div&gt;
       &lt;/form&gt;
       &lt;div class="form-group"&gt;
          &lt;label for="text"&gt;Quantity&lt;/label&gt;
          &lt;input type="text" name="qty" id="qty" placeholder="Quantity" required&gt;
       &lt;/div&gt;
       &lt;div class="form-group"&gt;
          &lt;label for="text"&gt;Item Description&lt;/label&gt;
          &lt;input type="text" name="Description" id="Description" placeholder="Description" required&gt;
        &lt;/div&gt;
        &lt;div class="form-group"&gt;
            &lt;label for="text"&gt;Date&lt;/label&gt;
            &lt;input type="date" name="ItemDate" id="ItemDate" required&gt;
        &lt;/div&gt;
        &lt;div class="form-group"&gt;
            &lt;label for="text"&gt;Supplier Name&lt;/label&gt;
            &lt;input type="text" name="SupplierName" id="SupplierName"  placeholder="Supplier Name" required&gt;
        &lt;/div&gt;
        &lt;div class="form-group"&gt;
            &lt;label for="text"&gt;Supplier Mail&lt;/label&gt;
            &lt;input type="text" name="SupplierMail" id="SupplierMail" placeholder="Supplier Mail" required&gt;
         &lt;/div&gt;&lt;br/&gt;
         &lt;div class="form-group"&gt;
             &lt;button id="submit" name="submit" value="insert"&gt;Insert&lt;/button&gt;
             &lt;?php 
                if(isset($_GET['insert']) &amp;&amp; ($_GET['insert'] == 1)){
                  echo "Inserted Successfully ";
                }else // This else part is running when the page is loading 
                  echo "Ooops..! Something went wrong.";
                }
             ?&gt;
         &lt;/div&gt;
       &lt;/form&gt; 
  </code></pre>