We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37161
    • 12 Posts
    I have been looking at Bob’s guides regarding http://bobsguides.com/custom-db-tables.html and related threads that I have found on this forum. I reading data fine and have successfully created new entries in my tables but have been unable to get the following code to update my tables.

    It seems to fail to get past the following if statement:

    if (empty($scriptProperties['rotaID'])) {


    Here is my complete snippet. Any suggestions would be greatly appreciated:

    <?php
    //begin snippet
    $scriptProperties['rotaID'] = !empty($_GET['rotaID']) ? intval($_GET['rotaID']) : '';
    if (empty($scriptProperties['rotaID'])) {
        // or you want to echo out any text
        //print_r($_POST);
        return '';
    }
    
    /* Get the existing Rota */
    
    $path = MODX_CORE_PATH . 'components/intranet_db_connection/';
    $modx->addPackage('intranet_db_connection',$path . 'model/','intranet_');
    $dayRota = $modx->getObject('ManagersRota', array('ID' => $scriptProperties['rotaID']));
    
    if (isset($_POST['submit']) && $_POST['submit'] == 'edit') {
    
    //die(print_r($_POST));
    
        $dayRota->set('Day', $_POST['Day']);
        $dayRota->set('senior_manager', $_POST['senior_manager']);
        $dayRota->set('duty_manager_day', $_POST['duty_manager_day']);
        $dayRota->set('duty_manager_night', $_POST['duty_manager_night']);
    
        if  ($dayRota->save()) {
            $output = "<p>Quote Created</p>";
            $output .= '<a href="[[~17? &scheme=`full`]]">back</a>';
        } else {
            $output = "<p>Error when trying to save</p>";
            $output .= '<a href="[[~17? &scheme=`full`]]">back</a>';
        }
    
    } else {
    
        /* Not a repost, just display the form */
    
        /* The second argument to getChunk() tells MODx to replace
         * the placeholders with the existing values
         */
        $output = $modx->getChunk('RotaTplEdit', $dayRota->toArray());
    }
    return $output;
    ?>
      • 22303 MODX Staff
      • 10,725 Posts
      Make sure you are calling your Snippet using the non-cacheable tag syntax, e.g.
      [[!rotaSnippet? &property=`value`]]

      Otherwise, the Snippet results are cached after the first request and it is not reprocessed. In order to process $_REQUEST parameters during a request cycle, a Snippet must be processed, and thus uncacheable.
        • 37161
        • 12 Posts
        Hi OpenGeek,

        Thank you for your reply. My snippet is being called uncached [[!managers_submit_rota]] and the code is definitely part working. The data from the previous page is definitely being parsed into the snippet as it produces a form which when submitted produces an array containing the new data.

        The problem seems to come when the submit button is pressed, I then get a blank page (caused by line 7
        return '';
        ) with no changes to my database. If I comment out line 7 and comment in line 6
        print_r($_POST);
        an array is returned containing the edited data submitted by the form.

        The problem seems to be that the on the second pass of the snippet it does not get past the first if statement so does not even attempt to update the database.

        Hope that makes sense and thanks again for you help.
          • 3749
          • 24,544 Posts
          Try using $_POST in your snippet instead of $_GET. Also, check the spelling and case of RotaID in your form (RotaId?). And if the ID can be 0, it will come up empty.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 37161
            • 12 Posts
            Hi BobRay,

            Thanks for the reply. I have been though the code and made sure all instances of rotaID are the same, which has made no difference sad

            Swapping $_POST for $_GET did not help much either. I just went straight to the blank page rather than the edit form.

            I think your right about the ID being 0, or as I am taking this to mean, rotaID not being passed when the submitting of the edit form. I am no convinced it is as simple this but not sure correct embarrassed

            If you could point me in the right direction it would be greatly appreciated. The edit form template chunk is here:

            <h3>[[+Day]]</h3>
            <form method="get" action="[[~[[*id]]]]">
                            <input type="hidden" name="rotaID" id="rotaID" value="[[+ID]]" />
                            <p>Senior Manager: <input type="text" name="senior_manager" id="senior_manager" value="[[+senior_manager]]" /></p>
                            <p>Duty Manager (Day): <input type="text" name="duty_manager_day" id="duty_manager_day" value="[[+duty_manager_day]]" /></p>
                            <p>Duty Manager (Night): <input type="text" name="duty_manager_night" id="duty_manager_night" value="[[+duty_manager_night]]" /></p>
            </form>
              • 3749
              • 24,544 Posts
              How are you setting the value of the [[+ID]] placeholder (and is the case correct).
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 37161
                • 12 Posts
                [[+ID]] is being pulled from the MySQL database and it is in the correct case.
                  • 22303 MODX Staff
                  • 10,725 Posts
                  I’m curious, are you using friendly URLs? If not, ID may not be the best choice here, since the $_GET parameter used by MODX is id. If that is not the problem, then make sure you are setting the placeholder for the ID properly and that the rendered page shows the proper value in that field.
                    • 37161
                    • 12 Posts
                    Sorry for the late reply. I have been very busy and struggling to turn on Furls, so have not been able to test this theory. I’ll get back to it next week hopefully and let you know how it goes.
                      • 37161
                      • 12 Posts
                      Hi Opengeek and BobRay,

                      I have been looking at this again for the past day or so with only small improvements. I have changed all instances of ID in my caode a tables to rotaID so it should now not be an issue with that. The entire snippet now seems to process but the new form data does not save to the database. When I die(print_r ($_POST the updated array displays so the seems to be an issue with the saving. I know I must be missing something simple but do not know what! Could there be something missing from my xpdo connection that would stop me udpating my database tables?

                      <?php
                      //begin snippet
                      $scriptProperties['rotaID'] = !empty($_GET['rotaID']) ? intval($_GET['rotaID']) : '';
                      
                      $database_server = 'localhost';
                      $database_type = 'mysql';
                      $dbase = 'Intranet';
                      $database_user = '*******';
                      $database_password = '********';
                      $table_prefix = 'intranet_';
                      
                      /* Get the existing Rota */
                      
                      $path = MODX_CORE_PATH . 'components/intranet_db_connection/';
                      
                      $xpdo = new xPDO('mysql:host=localhost;dbname=Intranet', $database_user,$database_password,$table_prefix);
                      
                      $modx->addPackage('intranet_db_connection',$path . 'model/','intranet_');
                      $dayRota = $modx->getObject('ManagersRota', array('rotaID'=> $scriptProperties['rotaID']));
                      
                      if (isset($_POST['editDay'])) {
                      
                      die(print_r($_POST));
                      
                          $dayRota->set('rotaID', $_POST['rotaID']);
                          $dayRota->set('Day', $_POST['Day']);
                          $dayRota->set('security_state', $_POST['security_state']);
                          $dayRota->set('senior_manager', $_POST['senior_manager']);
                          $dayRota->set('duty_adjudicator', $_POST['duty_adjudicator']);
                          $dayRota->set('duty_manager_day', $_POST['duty_manager_day']);
                          $dayRota->set('duty_manager_night', $_POST['duty_manager_night']);
                          $dayRota->set('on_call_controller', $_POST['on_call_controller']);
                          $dayRota->set('unlock_roll', $_POST['unlock_roll']);
                          $dayRota->set('duty_chaplain', $_POST['duty_chaplain']);
                      
                          if  ($dayRota->save()) {
                              $output = "<p>Quote Created</p>";
                              $output .= '<a href="[[~5? &scheme=`full`]]">back</a>';
                          } else {
                              $output = "<p>Error when trying to save</p>";
                              $output .= '<a href="[[~5? &scheme=`full`]]">back</a>';
                          }
                      
                      } else {
                      
                          /* Not a repost, just display the form */
                      
                          /* The second argument to getChunk() tells MODx to replace
                           * the placeholders with the existing values
                           */
                          $output = $modx->getChunk('RotaTplEdit', $dayRota->toArray());
                      }
                      return $output;