title: Editing MySQL row via POST form
body: <p>I've encountered a problem which i'll try to describe below...</p>
  
  <p>Let's say i've got a button for each table row (table is created by acquiring the data from the database...). the button sends a massive of values via post.</p>
  
  <pre><code>&lt;button value='$e-&gt;eventId|$e-&gt;eventType|$e-&gt;eventDate|$e-&gt;eventPrice|$e-&gt;eventDescr' name='editValue'&gt;EDIT&lt;/button&gt;
  </code></pre>
  
  <p>I've written a function to explode the values of the massive and fill the inputs on the same page.</p>
  
  <pre><code>if(!empty($_POST['editValue'])) {
      $editValue_fill = explode("|", $_POST["editValue"]);
      $eventId = $editValue_fill[0];
      $eventType = $editValue_fill[1];
      $eventDate = $editValue_fill[2];
      $eventPrice = $editValue_fill[3];
      $eventDescr = $editValue_fill[4];
  }
  </code></pre>
  
  <p>Next comes the function to edit the row in the database. </p>
  
  <pre><code>function editEvent ($id, $type, $date, $price, $descr){
  
      $mysqli = new mysqli($GLOBALS["serverHost"], $GLOBALS["serverUsername"], $GLOBALS['serverPassword'], $GLOBALS['dbName']);
      $stmt = $mysqli-&gt;prepare("UPDATE events_archive SET eventType='?', eventDate='?', eventPrice='?', eventDescr='?' WHERE id='?'");
      $stmt-&gt;bind_param("ssdss", $type, $date, $price, $descr, $id);
      $stmt-&gt;execute();
      header("Refresh:0");
  
      if($stmt-&gt;execute()){
          $eventNotice="Event successfully updated!";
      }else{
          $eventNotice = "Failed to save...";
      }
      return $eventNotice;
  }
  </code></pre>
  
  <p>And, of course, the usage of the function which apparently doesnt work. The page just refreshes and nothing happens. </p>
  
  <pre><code>if(empty($eventTypeError)&amp;&amp; empty($eventDateError)&amp;&amp; empty($eventPriceError) &amp;&amp; empty($eventDescrError)
      &amp;&amp; isset($_POST['eventType']) &amp;&amp; isset($_POST['eventDate']) &amp;&amp; isset ($_POST['eventPrice']) &amp;&amp; isset
      ($_POST['eventDescr']) &amp;&amp; !empty($eventId)){
      $eventNotice = editEvent($eventId, cleanInput($eventType), cleanInput($eventDate), cleanInput($eventPrice), cleanInput($eventDescr));
  }
  </code></pre>
  
  <p>So basically, gets values from the row -> fills them into inputs in the same page -> if $eventId is set then updates the row, if not - creates a new row (diff. function)</p>
  
  <p>Could anyone give me a tip or help solving the problem? I've been trying to understand the issue and failed dramatically...</p>
  
  <pre><code>&lt;?php
  require ("functions.php");
  
  var_dump($_POST);
  //kas on sisseloginud, kui ei ole siis
  //suunata login lehele
  
  //kas ?logout on aadressireal
  if (isset($_GET['logout'])){
      session_destroy();
      header("Location: login.php");
  }
  
  if (!isset ($_SESSION["userId"])){
      header("Location: login.php");
  }
  $confirm = "";
  $eventNotice = "";
  $eventTypeError = "";
  $eventDateError = '';
  $eventPriceError = '';
  $eventDescrError = '';
  $eventType = '';
  $eventDescr = '';
  $eventPrice = '';
  $eventDate = date("Y-m-d");
  
  
  
  if (isset ($_POST["eventType"])){
      if (empty($_POST['eventType'])){
          $eventTypeError = "Please choose the event type!";
      } else {
          $eventType = $_POST["eventType"];
      }
  }
  
  if (isset ($_POST ["eventDate"])){
      if (empty ($_POST ["eventDate"])){
          $eventDateError = "Please choose the date!";
      } else {
          $eventDate = $_POST["eventDate"];
      }
  }
  
  if (isset ($_POST ["eventPrice"])){
      if (empty ($_POST ["eventPrice"])){
          $eventPriceError = "Please type in the price!";
      } else {
          $eventPrice = $_POST["eventPrice"];
      }
  }
  
  if (isset ($_POST ["eventDescr"])){
      if (empty ($_POST ["eventDescr"])){
          $eventDescrError = "Please type in the description!";
      }elseif (strlen($_POST["eventDescr"])&lt; 10) {
          $eventDescrError = "Description must be longer than 10 symbols!";
          $eventDescr = $_POST['eventDescr'];
      }else{
          $eventDescr = $_POST['eventDescr'];
      }
  }
  
  $event = getAllEvents();
  
  if(!empty($_POST['delValue'])) {
      delEvent($_POST['delValue']);
  }
  
  if(!empty($_POST['editValue'])) {
      $editValue_fill = explode("|", $_POST["editValue"]);
      $eventId = $editValue_fill[0];
      $eventType = $editValue_fill[1];
      $eventDate = $editValue_fill[2];
      $eventPrice = $editValue_fill[3];
      $eventDescr = $editValue_fill[4];
      echo ($eventId);
      echo ($eventDate);
      echo ($eventPrice);
      echo ($eventType);
      echo ($eventDescr);
  }
  
  
  
  if(empty($eventTypeError)&amp;&amp; empty($eventDateError)&amp;&amp; empty($eventPriceError) &amp;&amp; empty($eventDescrError)
      &amp;&amp; isset($_POST['eventType']) &amp;&amp; isset($_POST['eventDate']) &amp;&amp; isset ($_POST['eventPrice']) &amp;&amp; isset
      ($_POST['eventDescr'])){
      if(isset($_POST['editValue'])){
          $eventNotice = editEvent($eventId, cleanInput($eventType), cleanInput($eventDate), cleanInput($eventPrice), cleanInput($eventDescr));
      }elseif(!isset($_POST['editValue'])){
          $eventNotice = newEvent(cleanInput($eventType), cleanInput($eventDate), cleanInput($eventPrice), cleanInput($eventDescr));
      }
  
  }
  
  ?&gt;
  
  
  &lt;html&gt;
  
  &lt;style&gt;
      @import "styles.css";
      ul.tab {
          list-style-type: none;
          margin: 0;
          padding: 0;
          overflow: hidden;
          border: 1px solid #ccc;
          background-color: #f1f1f1;
      }
      ul.tab li {float: left;}
      ul.tab li a {
          display: inline-block;
          color: black;
          text-align: center;
          padding: 14px 16px;
          text-decoration: none;
          transition: 0.3s;
          font-size: 17px;
      }
      ul.tab li a:hover {
          background-color: #ddd;
      }
      ul.tab li a:focus, .active {
          background-color: #ccc;
      }
      .tabcontent {
          display: none;
          padding: 6px 12px;
          border: 1px solid #ccc;
          border-top: none;
      }
  &lt;/style&gt;
  
  
  &lt;body&gt;
  &lt;form method ="post"&gt;
      &lt;table class="table1"&gt;
          &lt;tr&gt;
              &lt;td style="text-align:center"&gt;&lt;h1&gt;Data&lt;/h1&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;th&gt;&lt;h2&gt;Profile&lt;/h2&gt;&lt;/th&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;table class="table2"&gt;
                      &lt;tr&gt;
                          &lt;td colspan="3""&gt;Welcome &lt;?=$_SESSION['email'];?&gt;!&lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr&gt;
                          &lt;td colspan="3" style="text-align:center"&gt;&lt;a href="?logout=1"&gt;Log out&lt;/a&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/table&gt;
          &lt;tr&gt;
              &lt;td&gt;
  
                      &lt;tr&gt;
                          &lt;td&gt;
                              &lt;ul class="tab"&gt;
                                  &lt;li&gt;&lt;a href="javascript:void(0)" class="tablinks" onclick="openTab(event, 'Add/edit')" id="defaultOpen"&gt;Add/edit&lt;/a&gt;&lt;/li&gt;
                                  &lt;li&gt;&lt;a href="javascript:void(0)" class="tablinks" onclick="openTab(event, 'Archive')"&gt;Archive&lt;/a&gt;&lt;/li&gt;
                              &lt;/ul&gt;
  
                              &lt;div id="Add/edit" class="tabcontent"&gt;
                                  &lt;input type="hidden" value='eventId'&gt;
                                  &lt;table class="table2"&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;Event type:&lt;span class = 'redtext'&gt;*&lt;/span&gt;&lt;/td&gt;
                                          &lt;td style="text-align:left"&gt;
                                              &lt;select name="eventType"&gt;
  
                                                  &lt;?php if(empty($eventType)){?&gt;
                                                      &lt;option value="" selected&gt;Choose here&lt;/option&gt;
                                                  &lt;?php } else { ?&gt;
                                                      &lt;option value=""&gt;Choose here&lt;/option&gt;
                                                  &lt;?php } ?&gt;
  
                                                  &lt;?php if($eventType == "Planned service"){?&gt;
                                                      &lt;option value="Planned service" selected&gt;Planned service&lt;/option&gt;
                                                  &lt;?php } else { ?&gt;
                                                      &lt;option value="Planned service"&gt;Planned service&lt;/option&gt;
                                                  &lt;?php } ?&gt;
  
                                                  &lt;?php if($eventType == "Unplanned service"){?&gt;
                                                      &lt;option value="Unplanned service" selected&gt;Unplanned service&lt;/option&gt;
                                                  &lt;?php } else { ?&gt;
                                                      &lt;option value="Unplanned service"&gt;Unplanned service&lt;/option&gt;
                                                  &lt;?php } ?&gt;
  
                                                  &lt;?php if($eventType == "Fuel checks"){?&gt;
                                                      &lt;option value="Fuel checks" selected&gt;Fuel checks&lt;/option&gt;
                                                  &lt;?php } else { ?&gt;
                                                      &lt;option value="Fuel checks"&gt;Fuel checks&lt;/option&gt;
                                                  &lt;?php } ?&gt;
  
                                                  &lt;?php if($eventType == "Tuning"){?&gt;
                                                      &lt;option value="Tuning" selected&gt;Tuning&lt;/option&gt;
                                                  &lt;?php } else { ?&gt;
                                                      &lt;option value="Tuning"&gt;Tuning&lt;/option&gt;
                                                  &lt;?php } ?&gt;
  
                                                  &lt;?php if($eventType == "Car accident"){?&gt;
                                                      &lt;option value="Car accident" selected&gt;Car accident&lt;/option&gt;
                                                  &lt;?php } else { ?&gt;
                                                      &lt;option value="Car accident"&gt;Car accident&lt;/option&gt;
                                                  &lt;?php } ?&gt;
  
                                              &lt;/select&gt;
                                          &lt;/td&gt;
                                      &lt;/tr&gt;
                                      &lt;tr&gt;&lt;td colspan="3" class="redtext" style="text-align:center"&gt;&lt;?=$eventTypeError?&gt;&lt;/td&gt;&lt;/tr&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;Date:&lt;span class = 'redtext'&gt;*&lt;/span&gt;&lt;/td&gt;
                                          &lt;td style="text-align:left"&gt;&lt;input name="eventDate" type ="date" min="1900-01-01" max = "&lt;?=date('Y-m-d'); ?&gt;" value = "&lt;?=$eventDate?&gt;" placeholder="YYYY-MM-DD"&gt;&lt;/td&gt;
                                      &lt;/tr&gt;
                                      &lt;tr&gt;&lt;td colspan="3" class="redtext" style="text-align:center"&gt;&lt;?=$eventDateError?&gt;&lt;/td&gt;&lt;/tr&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;Price:&lt;span class = 'redtext'&gt;*&lt;/span&gt;&lt;/td&gt;
                                          &lt;td style="text-align:left"&gt;&lt;input type="text" name="eventPrice" placeholder="ex. 15.50" onkeypress="return onlyNumbersWithDot(event);" / value = "&lt;?=$eventPrice?&gt;"&gt;&lt;/td&gt;
  
                                          &lt;script type="text/javascript"&gt;
                                              function onlyNumbersWithDot(e) {
                                                  var charCode;
                                                  if (e.keyCode &gt; 0) {
                                                      charCode = e.which || e.keyCode;
                                                  }
                                                  else if (typeof (e.charCode) != "undefined") {
                                                      charCode = e.which || e.keyCode;
                                                  }
                                                  if (charCode == 46)
                                                      return true
                                                  if (charCode &gt; 31 &amp;&amp; (charCode &lt; 48 || charCode &gt; 57))
                                                      return false;
                                                  return true;
                                              }
                                          &lt;/script&gt;
  
                                      &lt;/tr&gt;
                                      &lt;tr&gt;&lt;td colspan="3" class="redtext" style="text-align:center"&gt;&lt;?=$eventPriceError?&gt;&lt;/td&gt;&lt;/tr&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;Description:&lt;span class = 'redtext'&gt;*&lt;/span&gt;&lt;/td&gt;
                                          &lt;td style="text-align:left"&gt;&lt;textarea name="eventDescr" cols="50" rows="10" placeholder="Describe event here..."&gt;&lt;?=$eventDescr?&gt;&lt;/textarea&gt;&lt;/td&gt;
                                      &lt;/tr&gt;
                                      &lt;tr&gt;&lt;td colspan="3" class="redtext" style="text-align:center"&gt;&lt;?=$eventDescrError?&gt;&lt;/td&gt;&lt;/tr&gt;
                                      &lt;tr&gt;
                                          &lt;td colspan="3" style="text-align:center"&gt;&lt;button type ="submit" value = "Submit"&gt;Save&lt;/button&gt;&lt;/td&gt;
                                      &lt;/tr&gt;
  
                                      &lt;tr&gt;
                                          &lt;td colspan="3" style="text-align:center"&gt;&lt;p class = "redtext"&gt;&lt;?=$eventNotice;?&gt;&lt;/p&gt;&lt;/td&gt;
                                      &lt;/tr&gt;
                                  &lt;/table&gt;
                              &lt;/div&gt;
  &lt;/form&gt;
  &lt;form method="post"&gt;
  
  
      &lt;div id="Archive" class="tabcontent"&gt;
          &lt;table class="table2"&gt;
              &lt;tr&gt;
                  &lt;td colspan="3""&gt;
                  &lt;?php
                  $html = "&lt;table&gt;";
                  $html .= "&lt;tr&gt;";
                  $html .= "&lt;th&gt;Event type&lt;/th&gt;";
                  $html .= "&lt;th&gt;Date&lt;/th&gt;";
                  $html .= "&lt;th&gt;Price(€)&lt;/th&gt;";
                  $html .= "&lt;th&gt;Description&lt;/th&gt;";
                  $html .= "&lt;th&gt;Delete&lt;/th&gt;";
                  $html .= "&lt;th&gt;Edit&lt;/th&gt;";
                  $html .= "&lt;/tr&gt;";
  
                  foreach($event as $e){
                      $html .= "&lt;tr&gt;";
                      $html .= "&lt;td&gt;$e-&gt;eventType&lt;/td&gt;";
                      $html .= "&lt;td&gt;$e-&gt;eventDate&lt;/td&gt;";
                      $html .= "&lt;td&gt;$e-&gt;eventPrice&lt;/td&gt;";
                      $html .= "&lt;td&gt;$e-&gt;eventDescr&lt;/td&gt;";
                      $html .= "&lt;td&gt;&lt;button style='border:none; background-color: transparent;' value='$e-&gt;eventId' name='delValue' onclick=\"return confirm('Do you really want to delete this row?')\"&gt;&lt;img src='delete.png' width='20' height='20'&gt;&lt;/button&gt;&lt;/td&gt;";
                      $html .= "&lt;td&gt;&lt;button style='border:none; background-color: transparent;' value='$e-&gt;eventId|$e-&gt;eventType|$e-&gt;eventDate|$e-&gt;eventPrice|$e-&gt;eventDescr' name='editValue'&gt;&lt;img src='edit.png' width='20' height='20'&gt;&lt;/button&gt;&lt;/td&gt;";
                      $html .= "&lt;/tr&gt;";
                  }
  
                  $html .= "&lt;/table&gt;";
                  echo $html;
  
                  ?&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
      &lt;/div&gt;
      &lt;script&gt;
          function openTab(evt, tabName) {
              var i, tabcontent, tablinks;
              tabcontent = document.getElementsByClassName("tabcontent");
              for (i = 0; i &lt; tabcontent.length; i++) {
                  tabcontent[i].style.display = "none";
              }
              tablinks = document.getElementsByClassName("tablinks");
              for (i = 0; i &lt; tablinks.length; i++) {
                  tablinks[i].className = tablinks[i].className.replace(" active", "");
              }
              document.getElementById(tabName).style.display = "block";
              evt.currentTarget.className += " active";
          }
  
          // Get the element with id="defaultOpen" and click on it
          document.getElementById("defaultOpen").click();
      &lt;/script&gt;
      &lt;/td&gt;
  
  
      &lt;/tr&gt;
  
      &lt;/table&gt;
      &lt;/td&gt;
      &lt;/tr&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  
  
  
  
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Looking forward to Your help!
  Best regards :)</p>