{"id":1738,"date":"2017-03-22T23:53:16","date_gmt":"2017-03-23T04:53:16","guid":{"rendered":"https:\/\/rpchurchill.com\/wordpress\/?p=1738"},"modified":"2017-03-24T02:03:19","modified_gmt":"2017-03-24T07:03:19","slug":"to-do-list-project-part-6","status":"publish","type":"post","link":"https:\/\/rpchurchill.com\/wordpress\/posts\/2017\/03\/22\/to-do-list-project-part-6\/","title":{"rendered":"To Do List Project: Part 6"},"content":{"rendered":"<p>Writing the ending time to each <code>ToDoSlice<\/code> item is more complex because we have to match to an existing record without benefit of having a unique ID to reference.  Instead, we use a combination of the <code>ToDoID<\/code> index and the starting time for the slice, which we known from the sending form.<\/p>\n<p>We start with the UpdateEndTime.php file.  We get the information for all four columns of the ToDoSlices and then try to update the end time and note fields, which is where the trouble lies.  Writing the ending time should be straightforward, since there should be no previous value for it in the record after it is created.  Updating the Note field, however, will present a problem if its text has <em>not<\/em> changed.  For some odd reason, MySQL&#8217;s <code>update<\/code> command won&#8217;t actually perform the update if a text value doesn&#8217;t change.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.rpchurchill.com\/images\/articles\/20170322_MySQL_update_weirdness.png\" width=\"400px\" \/><\/p>\n<p>You can verify this by manually issuing the same statement as this code generates, which will cause the command-line interface to report 1 row matched but 0 rows changed.  This operation won&#8217;t generate an error, it just won&#8217;t do anything at all, including update any other value in the record.  For that reason the code jumps through a lot of hoops to update the end time and note fields separately, see if either operation generated an error, and report back the appropriate information.  It&#8217;s probably possible that the end time will be properly updated while the note update fails (this would have to be something having to do with the MySQL host at a system level) so I should add some code to check for that condition.  There are other options as well, one of which could be to only update the end time if a check of the note field reveals that its text has not changed.<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n<?php\r\n  include('SQLFunctions.php');\r\n\r\n  if (!empty($_GET)) {\r\n    $tdRecordToDoID = $_GET['ToDoIDx'];\r\n    $tdNote = $_GET['Notex'];\r\n    $tdStartTime = $_GET['startTimex'];\r\n    $tdEndTime = date('Y-m-d H:i:s', time());\r\n    \r\n    $returnText = \"\";\r\n    $returnFlag = true;\r\n    \r\n    $link = connectDB();\r\n\r\n    $sql = \"update ToDoSlices \r\n            set Note='\".$tdNote.\r\n            \"' where recordToDoID=\".$tdRecordToDoID.\r\n            \" AND startTime='\".$tdStartTime.\"';\";\r\n  \r\n    if (!mysqli_query($link, $sql)) {\r\n      $returnFlag = false;\r\n      $returnText .= \"Note Error: \".$sql.\"<br>\".mysqli_error($link).\" \";\r\n    }\r\n    \r\n    $sql = \"update ToDoSlices \r\n            set endTime='\".$tdEndTime.\r\n            \"' where recordToDoID=\".$tdRecordToDoID.\r\n            \" AND startTime='\".$tdStartTime.\"';\";\r\n\r\n    if (!mysqli_query($link, $sql)) {\r\n      $returnFlag = false;\r\n      $returnText .= \"End Time Error: \".$sql.\"<br>\".mysqli_error($link).\" \";\r\n    }\r\n\r\n    if ($returnFlag) {\r\n      echo $tdEndTime;\r\n    } else {\r\n      echo $returnText;\r\n    }\r\n    \r\n    mysqli_close( $link );\r\n  }\r\n\r\n?>\r\n<\/pre>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n      echo \"    function endSliceAjax() {\";\r\n      echo \"      var xmlhttp = new XMLHttpRequest();\";\r\n      echo \"      var handle = document.getElementById('endButton');\";\r\n      echo \"      var parentHandle = handle.parentNode;\";\r\n      echo \"      var noteHandle = document.getElementById('nextSliceNoteID');\";\r\n      echo \"      var noteValue = noteHandle.value;\";\r\n      echo \"      sliceIndex = parentHandle.previousElementSibling.previousElementSibling.innerHTML;\";\r\n      echo \"      var startTime = parentHandle.previousElementSibling.innerHTML;\";\r\n      echo \"      xmlhttp.onreadystatechange = function() {\";\r\n      echo \"        if (this.readyState == 4) {\";\r\n      echo \"          if (this.status == 200) {\";\r\n      echo \"            if (this.responseText.search('Error: ') < 0) {\";\r\n      echo \"              alert('response: '+this.responseText);\";\r\n      echo \"              parentHandle.innerHTML = this.responseText;\";\r\n      echo \"              noteHandle.parentElement.innerHTML = noteValue;\";\r\n      echo \"              addTableRow();\";\r\n      echo \"            } else {\";\r\n      echo \"              alert(this.responseText);\";\r\n      echo \"            }\";\r\n      echo \"          } else {\";\r\n      echo \"            alert('AJAX error encountered');\";\r\n      echo \"          }\";\r\n      echo \"        }\";\r\n      echo \"      };\";\r\n      echo \"      xmlhttp.open('GET','UpdateEndTime.php?ToDoIDx='+$ToDoID+'&#038;startTimex='+startTime+'&#038;Notex='+noteValue, true);\";\r\n      echo \"      xmlhttp.send();\";\r\n      echo \"    }\";\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Writing the ending time to each ToDoSlice item is more complex because we have to match to an existing record without benefit of having a unique ID to reference. Instead, we use a combination of the ToDoID index and the &hellip; <a href=\"https:\/\/rpchurchill.com\/wordpress\/posts\/2017\/03\/22\/to-do-list-project-part-6\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[169,49,82,165,167],"_links":{"self":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/1738"}],"collection":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1738"}],"version-history":[{"count":1,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/1738\/revisions"}],"predecessor-version":[{"id":1739,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/1738\/revisions\/1739"}],"wp:attachment":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}