title: Insert form does not works as the edit query is coded in single form
body: <p>I was converting my existing PHP MySQLi code to PDO. In it, I made a single form that will add and update the data. My edit is working without any issue but whenever I select to add form an error appears for invalid argument supplied for foreach loop. And due to this add is not working. Here I made functions for add, update and fetch the data by id. The function for add is:</p>
  <pre><code>    function addSchool($schoolName) {
      global $connect;
      $sql = &quot;INSERT INTO school (schoolName) VALUES (:schoolName)&quot;;
      $query = $connect -&gt; prepare($sql);
      $query-&gt;bindParam(':schoolName',$schoolName,PDO::PARAM_STR);
      $query -&gt; execute();
      if($query){
          return true;
      }else{
          return false;
      }
  }
  </code></pre>
  <p>The function for edit is:</p>
  <pre><code>function editSchool($schoolId, $schoolName)
  {
      global $connect;
      $sql = &quot;update school set schoolName = :schoolName where schoolId = :schoolId &quot;;
      $query = $connect-&gt;prepare($sql);
      $query -&gt; bindParam(':schoolId', $schoolId, PDO::PARAM_INT);
      $query -&gt; bindParam(':schoolName' , $schoolName , PDO::PARAM_STR);
      $query -&gt; execute();
      if($query){
          return true;
      }else{
          return false;
      }
  }
  </code></pre>
  <p>And the function used to get data by id for edit purpose is:</p>
  <pre><code>    function getSchoolById($schoolId)
  {
      global $connect;
      $sql = &quot;SELECT * FROM school where schoolId = :schoolId&quot;;
      // preparing the query
      $query = $connect-&gt;prepare($sql);
      $query-&gt;bindParam(':schoolId',$schoolId,PDO::PARAM_INT);
      $query-&gt;execute();
      $result = $query -&gt; fetchAll(PDO::FETCH_OBJ);
      return $result;
    
  }
  </code></pre>
  <p>The way I utilized above functions in HTML/PHP file is :</p>
  <pre><code>&lt;?php
  include_once(&quot;includes/connect.php&quot;);
  include_once(&quot;function.php&quot;);
  
  $editState = false;
  $schoolById = &quot;&quot;;
  if (isset($_POST['add'])) {
      $schoolName = $_POST['schoolName'];
      $saveSchool = addSchool($schoolName);
      $lastInsertId = $connect-&gt;lastInsertId();
      if ($lastInsertId) {
          echo &quot;&lt;div class='alert alert-success alert-dismissible fade show'&gt;
          &lt;strong&gt;Success!&lt;/strong&gt; School Added.
          &lt;button type='button' class='close' data-dismiss='alert'&gt;&amp;times;&lt;/button&gt;
      &lt;/div&gt;&quot;;
      } else {
          echo &quot;&lt;div class='alert alert-danger alert-dismissible fade show'&gt;
          &lt;strong&gt;Error!&lt;/strong&gt; School not added.
          &lt;button type='button' class='close' data-dismiss='alert'&gt;&amp;times;&lt;/button&gt;
      &lt;/div&gt;&quot;;
      }
      
      }
      if (isset($_GET['schoolId'])) {
          $schoolId = $_GET['schoolId'];
          $editState = true;
          $schoolById = getSchoolById($schoolId);
      } 
  
  
  if (isset($_POST['edit'])) {
   
      $schoolName = $_POST['schoolName'];
      $updateSchool = editSchool($schoolId, $schoolName);
      if ($updateSchool) {
          echo &quot;&lt;div class='alert alert-success alert-dismissible fade show'&gt;
          &lt;strong&gt;Success!&lt;/strong&gt; School Updated.
          &lt;button type='button' class='close' data-dismiss='alert'&gt;&amp;times;&lt;/button&gt;
      &lt;/div&gt;&quot;;
      } else {
          echo &quot;&lt;div class='alert alert-danger alert-dismissible fade show'&gt;
          &lt;strong&gt;Error!&lt;/strong&gt; School not updated.
          &lt;button type='button' class='close' data-dismiss='alert'&gt;&amp;times;&lt;/button&gt;
      &lt;/div&gt;&quot;;
      }
  } 
  
  ?&gt;
  </code></pre>
  <p>And below is my form for add and update. It is the single form through which upon selecting add the add opens and upon selection of edit, edit form appears:</p>
  <pre><code>&lt;div class=&quot;container-fluid&quot;&gt;
              &lt;?php 
              foreach ($schoolById as $school) {  
              ?&gt;
              &lt;form action=&quot;&quot; method=&quot;POST&quot;&gt;
                  &lt;div class=&quot;form-group&quot;&gt;
                      &lt;label&gt;School Name&lt;/label&gt;
                      &lt;input type=&quot;text&quot; class=&quot;col-sm-4 form-control&quot; name=&quot;schoolName&quot; value=&quot;&lt;?php echo $school-&gt;schoolName;  ?&gt;&quot;&gt;
                  &lt;/div&gt;
         
                  &lt;div class=&quot;form-group&quot;&gt;
                      &lt;?php if ($editState == true) : ?&gt;
                          &lt;button class=&quot;btn btn-primary&quot; type=&quot;submit&quot; name=&quot;edit&quot;&gt;Update School&lt;/button&gt;
                      &lt;?php else : ?&gt;
                          &lt;button class=&quot;btn btn-primary&quot; type=&quot;submit&quot; name=&quot;add&quot;&gt;Add School&lt;/button&gt;
                      &lt;?php endif ?&gt;
                  &lt;/div&gt;
              &lt;/form&gt;
          &lt;/div&gt; 
          &lt;?php      
           }
          ?&gt;
  </code></pre>
  <p>I am not getting the point of how to remove this invalid foreach argument. Moreover, when <code>$schoolById</code> is declared then errors are removed from add school form but form disappears and the edit school form works smoothly. Now I want to know that how to get this issue resolved.</p>