On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • Hi - This is a part of a php processing script that I am trying to update the database with on the condition that the $RSAchecked variable equals 1 and ignore the block if not. Same for the second block with the $RSGchecked variable.
    The original MySQL update statement below works fine without the if (....) part around it, but with the if (....), the database doesn't update. However, the rest of the processing script continues to complete (ie; sends emails etc;)
    Any guidance on my syntax would be greatly received.

    require_once('../manager/includes/config.inc.php');
    require_once('../manager/includes/document.parser.class.inc.php');
    $modx = new DocumentParser;
    
    // ******** UPDATE SESSION 1 PLACES **********
    
    if (isset($RSAchecked) && ($RSAchecked === 1)) {
    
    $update_seats1 = update_places1($id1, $seats);	
    function update_places1($id1, $seats) {        
    	global $modx;		
    	$table = "`prefix_modx`.`xtra_session`";	    
    	$update_seats1 = '`places` -' .$seats;		
    	$result = $modx->db->update( '`places` = ' . $update_seats1, $table, '`id` = ' . intval($id1, 10) . ' AND `places` >= ' . $seats );        
    	return $result;	         // Returns 'true' on success, 'false' on failure.
      }
    }
    
    // ******** UPDATE SESSION 2 PLACES **********
    
    if (isset($RSGchecked) && ($RSGchecked === 1))  {
    	
    $update_seats2 = update_places2($id2, $seats);	
    function update_places2($id2, $seats) {        
    	global $modx;		
    	$table = "`prefix_modx`.`xtra_session`";	    
    	$update_seats2 = '`places` -' .$seats;		
    	$result = $modx->db->update( '`places` = ' . $update_seats2, $table, '`id` = ' . intval($id2, 10) . ' AND `places` >= ' . $seats );        
    	return $result;         // Returns 'true' on success, 'false' on failure.
      }
    }


    Rest of the processing script continues here....
    • Here is the first thing I would do:

      require_once('../manager/includes/config.inc.php');
      require_once('../manager/includes/document.parser.class.inc.php');
      $modx = new DocumentParser;
      
      if (!function_exists('update_places1')) {
          function update_places1($id1, $seats) {        
              global $modx;       
              $table = "`prefix_modx`.`xtra_session`";        
              $update_seats1 = '`places` -' .$seats;      
              $result = $modx->db->update( '`places` = ' . $update_seats1, $table, '`id` = ' . intval($id1, 10) . ' AND `places` >= ' . $seats );        
              return $result; // Returns 'true' on success, 'false' on failure.
            }
          }
      }
      if (!function_exists('update_places2')) {
          function update_places2($id2, $seats) {        
              global $modx;       
              $table = "`prefix_modx`.`xtra_session`";        
              $update_seats2 = '`places` -' .$seats;      
              $result = $modx->db->update( '`places` = ' . $update_seats2, $table, '`id` = ' . intval($id2, 10) . ' AND `places` >= ' . $seats );        
              return $result; // Returns 'true' on success, 'false' on failure.
            }
          }
      }
      
      // ******** UPDATE SESSION 1 PLACES **********
      
      if (isset($RSAchecked) && ($RSAchecked === 1)) {
          $update_seats1 = update_places1($id1, $seats);
      }
      
      // ******** UPDATE SESSION 2 PLACES **********
       
      if (isset($RSGchecked) && ($RSGchecked === 1))  {   
          $update_seats2 = update_places2($id2, $seats);
      }  
        @hawproductions | http://mrhaw.com/

        Infograph: MODX Advanced Install in 7 steps:
        http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

        Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
        http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
      • Hi MrHaw - thank you for your reply. I have tried your code suggestions with several variations yet I still cannot get the database to update.(Question - in the if(isset($RSAchecked)....etc function above - shouldn't the $update_seats1 = part actually be $update_places1 ...??? and there appears to be an extra }.)

        I would be grateful if you would advise if I understand your logic correctly -

        UPDATE SESSION 1 PLACES FUNCTION - If RSAchecked is set and has the value of 1 then it will set the function $update_seats1 so that it exists.
        Then the
        if (!function_exists('update_places1'))
        code block will return FALSE if not set and do nothing, otherwise if it is set then the MySQL statement will run?
        When you say, 'This is the first thing I would do...' , is there more, like a return or something I am missing?

        The entire script completes with no errors and emails are sent but the database is not updated.
        I am still on a learning curve with this so I appreciate your patience and help. [ed. note: Twobears last edited this post 5 years, 4 months ago.]
        • You can remove the function_exists()

          I can't see all code being used in your script but...

          Modify the code to this

          // ******** UPDATE SESSION 1 PLACES **********
           
          if (isset($RSAchecked) && ($RSAchecked === 1)) {
              // update_places1($id1, $seats);
              print "RSAchecked variable is set and id1 equals ". $id1 " | seats equals ". $seats;
          }
           
          // ******** UPDATE SESSION 2 PLACES **********
            
          if (isset($RSGchecked) && ($RSGchecked === 1))  {   
              //update_places2($id2, $seats);
              print "RSGchecked variable is set and id2 equals ". $id2 " | seats equals ". $seats;
          }


          if that works and you see the values being correct then try run

          // ******** UPDATE SESSION 1 PLACES **********
           
          if (isset($RSAchecked) && ($RSAchecked === 1)) {
              update_places1($id1, $seats);
          }
           
          // ******** UPDATE SESSION 2 PLACES **********
            
          if (isset($RSGchecked) && ($RSGchecked === 1))  {   
              update_places2($id2, $seats);
          }



          When I create a CRUD form. I write 3 functions: Create, Update and Delete. You are just updating existing fields.
            @hawproductions | http://mrhaw.com/

            Infograph: MODX Advanced Install in 7 steps:
            http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

            Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
            http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower