We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7231
    • 4,205 Posts
    Also make it is spelled out the same as in your snippet panel. Snippets are case sensitive.
      [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

      Something is happening here, but you don't know what it is.
      Do you, Mr. Jones? - [bob dylan]
      • 20744
      • 19 Posts
      Hi, thanks for the help. No luck so far. I have a db table called modx_form_results. I tried [[eForm2db]] [!eForm2db!] and [!eForm2db?!] but still no luck. Same error occurs. I double checked my fields and they seem fine.

      Here is the code in the chunk in case I am doing something stupid! Taken from Pixelchutes post.


      <?php
      function eForm2db ( &$fields )
      {
      /*---------------------------------------------------------------
      eForm2db
      Version: 0.1 [Beta 1]
      Author: pixelchutes
      ---------------------------------------------------------------
      Requirements:
      eForm 1.4+
      ---------------------------------------------------------------
      Use:
      - Takes validated eForm submittal data for use with the MODx DBAPI extender class.
      - Easily process database records referencing your form submission data:
      + INSERT new records to a table of your choice
      + UPDATE existing records if you’d prefer
      + DELETE records based on form submittal criteria
      + Form a query and return its record set as XML using $modx->db->getXML
      + Query a table’s MetaData using $modx->db->getTableMetaData, followed by an update query on only the form fields that are named after table columns!
      + and much more!
      ---------------------------------------------------------------*/

      // Bring needed resources into scope
      global $modx, $table_prefix;

      // Init our array
      $dbTable = array(); // key = DB Column; Value = Insert/Update value

      // Insert field/value pairs to insert/update in our table
      $dbTable[con_name] = $fields[name]; // Name Field
      $dbTable[con_email] = $fields[email]; //email
      $dbTable[con_company] = $fields[company]; //company
      $dbTable[con_query] = $fields[query]; //query


      // INSERT - $dbQuery = $modx->db->insert( $dbTable, $table_prefix . ’modx_form_results’ );
      // UPDATE - $dbQuery = $modx->db->update( $dbTable, $table_prefix . ’modx_form_results’ );
      // DELETE - $dbQuery = $modx->db->delete( $table_prefix . ’deleteFromTableName’, ’some_field = 1 AND name=\’’.$dbTable[modx_form_results].’\’’, ’’ );
      // etc...

      // Run the db insert query
      $dbQuery = $modx->db->insert( $dbTable, $table_prefix . ’modx_form_results’ );

      return true;
      }
      // Return empty string
      return ’’;
      ?>

      Thanks again.
        • 26182
        • 164 Posts
        Hey,

        I tried this code based on PixelChutes code and now it won’t even enter into the database:

        <?php
        function eForm2db( &$fields )
        	{
        		/*---------------------------------------------------------------		
        		eForm2db 
        		Version: 0.1 [Beta 1]
        		Author: pixelchutes
        		---------------------------------------------------------------
        		Requirements:
        			eForm 1.4+
        		---------------------------------------------------------------
        		Use:
        			- Takes validated eForm submittal data for use with the MODx DBAPI extender class.								
        			- Easily process database records referencing your form submission data:				  
        			  + INSERT new records to a table of your choice				  
        			  + UPDATE existing records if you'd prefer				  
        			  + DELETE records based on form submittal criteria				  
        			  + Form a query and return its record set as XML using $modx->db->getXML				  
        			  + Query a table's MetaData using $modx->db->getTableMetaData, followed by an update query
        			    on only the form fields that are named after table columns!				    
        			  + and much more! 
        		---------------------------------------------------------------*/
        
        	// Bring needed resources into scope
        	global $modx, $table_prefix;
           
        	$sql = ("select * from `competitionEntries` where `email` = '".$fields['email']."'");
        
                $result = $modx->db->query($sql);
                return $result;
        
                $num = mysql_num_rows($result);
               
                       
                       if( $num > 0 )
                       {
                           // Set placeholders
                           $chunkArr = array('dupe_entry' => 'It appears you have already entered the contest. Only one entry per person is allowed. If you have a question or feel there is a problem please <a href="index.php?id=5">click here to contact us</a>. Thank you.');
        
                      	  	//Output the dupe alert
                            //echo $modx->parseChunk( 'dupe_entry_tpl', $chunkArr, '[+', '+]' );
                            echo 'It appears you have already entered the contest. Only one entry per person is allowed';
                          	// Prevent form load
                         	return true;
        		}else
        		{
        
        		        // Init our array
        		        $dbTable = array(); // key = DB Column; Value = Insert/Update value
        
        		        // Insert field/value pairs to insert/update in our table
        			$dbTable[firstName] = $fields[firstName];
        			$dbTable[lastName] = $fields[lastName];
        			$dbTable[address] = $fields[address];
        			$dbTable[suburb] = $fields[suburb];
        		        $dbTable[state] = $fields[state];
                        	$dbTable[postcode] = $fields[postcode];
                        	$dbTable[email] = $fields[email];
                        	$dbTable[daytimePhone] = $fields[daytimePhone];
                        	$dbTable[mobilePhone] = $fields[mobilePhone];
                        	$dbTable[heardAbout] = $fields[heardAbout];
                        	$dbTable[termsConditions] = $fields[termsConditions];
                        	$dbTable[subscribe] = $fields[subscribe];
        			$dbTable[entryDate] = date( 'YmdHis', strtotime( $fields[postdate] ) ); // Message the postdate timestamp to be MySQL insert friendly
        
        			// INSERT - $dbQuery = $modx->db->insert( $dbTable, $table_prefix . 'competitionEntries' );
        			// UPDATE - $dbQuery = $modx->db->update( $dbTable, $table_prefix . 'competitionEntries' );
        			// DELETE - $dbQuery = $modx->db->delete( $table_prefix . 'competitionEntries', 'some_field = 1 AND name=\''.$dbTable[name].'\'', '' );
        			// etc...
        				
        			// Run the db insert query
        	       		//$dbQuery = $modx->db->insert( $dbTable, $table_prefix . 'competitionEntries' );	
                       		$dbQuery = $modx->db->insert( $dbTable, 'competitionEntries' );		
        		}//end if else
        		
        
        
        	return true;
        	}
        	// Return empty string
        	return '';
        ?>


        Can someone please tell me what I’m doing wrong?

        Thanks!

        Sarah
          • 7231
          • 4,205 Posts
          Are you sure that you have a table in your db name competitionEntries? Replace this with the name of the table that you are using and remember that it is case sensitive.

          Also start with the basic call and add the check to see if an entry already exists after you are confident that the insert part is working.

          Your first attempt seems that it should work but the insert statement has a mistake:
          $dbQuery = $modx->db->insert( $dbTable, $table_prefix . 'modx_form_results' );  
          You are adding the table prefix twice, the resulting table name would be modx_modx_form_results.
            [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

            Something is happening here, but you don&#39;t know what it is.
            Do you, Mr. Jones? - [bob dylan]
            • 26182
            • 164 Posts
            hey,

            the table competitionEntries exists in the database. the insert is working - the first one is commented out as i modified the db insert to omit the table prefix as the table doesn’t have the modx prefix.

            it still isn’t working.

            i’m not sure whether i should put the check in a function or not...

            maybe i’ll do some testing in a different area.

            sarah


              • 26182
              • 164 Posts
              I think the function is quitting at the return $result line.

              Is there some way to get around this?

              Please help!



                • 26182
                • 164 Posts
                are you putting the code in a chunk or a snippet? it needs to be in a snippet.
                  • 26182
                  • 164 Posts
                  Ok, I’ve tried so many different ways and I’m still not getting any headway.

                  You can see from the commented out code all the methods I’ve tried.

                  <?php
                  function eForm2db( &$fields )
                  	{
                  		/*---------------------------------------------------------------		
                  		eForm2db 
                  		Version: 0.1 [Beta 1]
                  		Author: pixelchutes
                  		---------------------------------------------------------------
                  		Requirements:
                  			eForm 1.4+
                  		---------------------------------------------------------------
                  		Use:
                  			- Takes validated eForm submittal data for use with the MODx DBAPI extender class.								
                  			- Easily process database records referencing your form submission data:				  
                  			  + INSERT new records to a table of your choice				  
                  			  + UPDATE existing records if you'd prefer				  
                  			  + DELETE records based on form submittal criteria				  
                  			  + Form a query and return its record set as XML using $modx->db->getXML				  
                  			  + Query a table's MetaData using $modx->db->getTableMetaData, followed by an update query
                  			    on only the form fields that are named after table columns!				    
                  			  + and much more! 
                  		---------------------------------------------------------------*/
                  
                  	// Bring needed resources into scope
                  	global $modx, $table_prefix;
                  
                          $num = 0;
                          //$tmpEmail = $fields[email];
                  	//$sql = ("select * from `competitionEntries` where `email` = '".$fields['email']."'");
                          $sql = ("select * from `competitionEntries` where `email` = '$tmpEmail'");
                          /*$sql = ("select * from `competitionEntries`");
                  		while( $row = $modx->fetchRow($sql) ){
                                          $storeEmail = $row->email;
                                          if ($storeEmail== $tmpEmail)
                                          {
                                                 $num ++;
                                          }else
                                          {
                                       
                                          }
                  
                                  }
                          */
                          $result = $modx->db->query($sql);
                          $num = $modx->db->getRecordCount($result);
                          //$result= $result->fetchAll(PDO_FETCH_ASSOC);
                          //return $result;
                          //$num = mysql_num_rows($result);
                         
                                 
                                 if ($num < 0)
                                 {
                                     // Set placeholders
                                     $chunkArr = array('dupe_entry' => 'It appears you have already entered the contest. Only one entry per person is allowed. If you have a question or feel there is a problem please <a href="index.php?id=5">click here to contact us</a>. Thank you.');
                  
                                	     //Output the dupe alert
                                      echo $modx->parseChunk( 'dupe_entry_tpl', $chunkArr, '[+', '+]' );
                                      //echo 'It appears you have already entered the contest. Only one entry per person is allowed';
                                    	// Prevent form load
                                   	return true;
                  		}else
                  		{
                  
                  		        // Init our array
                  		        $dbTable = array(); // key = DB Column; Value = Insert/Update value
                  
                  		        // Insert field/value pairs to insert/update in our table
                  			$dbTable[firstName] = $fields[firstName];
                  			$dbTable[lastName] = $fields[lastName];
                  			$dbTable[address] = $fields[address];
                  			$dbTable[suburb] = $fields[suburb];
                  		        $dbTable[state] = $fields[state];
                                  	$dbTable[postcode] = $fields[postcode];
                                  	$dbTable[email] = $fields[email];
                                  	$dbTable[daytimePhone] = $fields[daytimePhone];
                                  	$dbTable[mobilePhone] = $fields[mobilePhone];
                                  	$dbTable[heardAbout] = $fields[heardAbout];
                                  	$dbTable[termsConditions] = $fields[termsConditions];
                                  	$dbTable[subscribe] = $fields[subscribe];
                  			$dbTable[entryDate] = date( 'YmdHis', strtotime( $fields[postdate] ) ); // Message the postdate timestamp to be MySQL insert friendly
                  
                  			// INSERT - $dbQuery = $modx->db->insert( $dbTable, $table_prefix . 'competitionEntries' );
                  			// UPDATE - $dbQuery = $modx->db->update( $dbTable, $table_prefix . 'competitionEntries' );
                  			// DELETE - $dbQuery = $modx->db->delete( $table_prefix . 'competitionEntries', 'some_field = 1 AND name=\''.$dbTable[name].'\'', '' );
                  			// etc...
                  				
                  			// Run the db insert query
                  	       		//$dbQuery = $modx->db->insert( $dbTable, $table_prefix . 'competitionEntries' );	
                                 		$dbQuery = $modx->db->insert( $dbTable, 'competitionEntries' );		
                  		}//end if else
                  		
                  
                  
                  	return true;
                  	}
                  	// Return empty string
                  	//return '';
                  ?>
                    • 26182
                    • 164 Posts
                    These are my snippet calls:

                    [!eForm2db?!]
                    [!eForm? &formid=`competitionForm` &noemail=`true` &eFormOnBeforeMailSent=`eForm2db` &tpl=`eCompetitionForm` &report=`eCompetitionFormReport` &gotoid=`22` &vericode=`1` &subject=`Competition entry`!]

                    I’ve defined the email as the unique key in the table - it is always mysql that returns the error not eForm2db.

                    I’m at a loss. I’ve tried so many things.

                    Anybody?
                      • 20744
                      • 19 Posts
                      I removed the modx_ from the db table name but still getting the same problem. Nothing gets written to the database.

                      I am also getting a problem with the mail function on the form. The error I get is "Mailer was unable to send mail Could not instantiate mail function." My understanding is that the two functions are separate and that the write to db should work even if the sendmail doesn’t is that correct?

                      I read that the issue of PHP mail is often related to security issue. I tried a simple page with PHP mail and it seems to work fine as I receive the email. Any suggestions?