Topic: [Snippet] eForm2db 0.1BETA - Manipulate DB records via eForm and MODx DBAPI  (Read 72323 times)

Pages: 1 ... 8 9 [10]   Go Down

#181: 8-Jan-2010, 07:48 PM

jrad
Posts: 12

eform2db works great.

1.  i'm able to save my data to the database, but after the form is posted i go back to the same page without showing the updated information stored in the table.  It's not until i refresh the page that i see the updated info.

is there a way i can have the page refresh the data between the time the form posts and the page is rendered with the updated information?  am i missing something.


#182: 10-Jan-2010, 06:37 PM


Andrey Nagikh
Posts: 499

Sorry for my ugly English

WWW
Is this page cachable?

#183: 18-Jan-2010, 08:03 AM


joshlfisher
Posts: 249

WWW
Just to Clarify:

the &eFormOnBeforeMailSent=`` param needs to contain the name of the FUNCTION from the eform2db snippet, right?
Visit Knight Technology Solutions at http://knightechnologysolutions.com

#184: 20-Jan-2010, 12:10 AM


Andrey Nagikh
Posts: 499

Sorry for my ugly English

WWW
Yes, it's the name of PHP function.

#185: 27-Jan-2010, 02:16 PM

betacar
Posts: 6

Good day,
So I've been using eForm2db in some of my MODx forms. However, when I work with form fields with the same name (duplicated fields), is confusing to get it's values and insert them in DB table.

Here's an example:
Code:
&
<?php function insertPensum$fields ) {
      global 
$modx$table_prefix;
      
$tbl_carrera $table_prefix.'carreras';
      
$tbl_catedra $table_prefix.'catedras';
      
define('IDSTAT',1);

      
// Asignacion de fields para carreras
      
$dbTable = array();

      
$dbTable[id_carrer] = NULL;
      
$dbTable[id_stat] = IDSTAT;
      
$dbTable[id_uni] = $fields[id_uni];
      
$dbTable[carrer_descp] = $fields[carrer_descp];

      
// Insercion de datos en tabla de carreras
      
$dbQuery $modx->db->insert($dbTable$tbl_carrera); //This query works perfectly.

      //Here I search the recent inserted ID.
      
$allFields $modx->db->select('id_carrer'$tbl_carrera);
      
$rowSize $modx->db->getRecordCount($allFields);

      if(
$rowSize 1) {
          
$i=1;
          while(
$row $modx->db->getRow($allFields)) {
              
$i++;
              if(
$i == $rowSize$lastID $row['id_carrer'];
          }
      } elseif(
$rowSize == 1) {
          
$row $modx->db->getRow($allFields);
          
$lastID $row['id_carrer'];
      } else echo 
'No hay carreras insertadas. Por favor verifique el insert de carreras en el snippet';

      
//These fields are the duplicated ones. Is basicatly a form to insert 
      
$idMat = array(implode(','$fields[id_mat]));
      
$matDesc = array(implode(','$fields[mat_desc]));
      
$matSem = array(implode(','$fields[mat_sem]));
      
$matCredit = array(implode(','$fields[mat_credit]));
      
$matBasico = array(implode(','$fields["mat_basico"]));
      
      
$fldSize count($idMat);//I don't know if this is right: Here I get the length of array.

      
for($i=0$i <= $fldSize$i++) {
          if(
$i == ($fldSize-1)) {
              
$fldOuput .= '('.$idMat[$i].', '.IDSTAT.', '.$lastID.', `'.$matDesc[$i].'`, '.$matSem[$i].', '.$matCredit[$i].', '.$matBasico[$i].')';
          } elseif((
$i == $fldSize) || (!$idMat[$i])) {
              
$fldOuput .= '';
          } else 
$fldOuput .= '('.$idMat[$i].', '.IDSTAT.', '.$lastID.', `'.$matDesc[$i].'`, '.$matSem[$i].', '.$matCredit[$i].', '.$matBasico[$i].'), ';
      }

      
//Inserte the data in the table
      
$dbQuery $modx->db->query('INSERT INTO '.$tbl_catedra.' (id_mat, id_stat, id_carrer, mat_desc, mat_sem, mat_credit, mat_basico) VALUES '.$fldOuput);

      return 
true;
}
?>

The following is the error MODx gives back, when I tried to submit 2 subjects:
Quote
« MODx Parse Error »
MODx encountered the following error while attempting to parse the requested resource:
« Execution of a query to the database failed - Column count doesn't match value count at row 1 »
      SQL: INSERT INTO ugmodx_catedras (id_mat, id_stat, id_carrer, mat_desc, mat_sem, mat_credit, mat_basico) VALUES (12001114,12001213, 1, 2, `Matemática I,Computación Básica I`, 1,1, 4,3, 1)

The form chunk. I duplicate the fields by the jQuery append function:
Code:
            <h2>Nuevo pensum</h2>
            <form method="post" action="[~[*id*]~]" id="newPensum" name="newPensum">
                <fieldset>
                    <h4>Sede y carrera</h4>
                    <input name="newPensum" type="hidden" value="NewPensum" />
                    <div id="Labels"><label for="id_uni">Sede:</label></div>
                    <select name="id_uni" id="id_uni" tabindex="0" eform="Sede::1:Eliga una sede de la carrera:#SELECT id_uni FROM {PREFIX}sedes WHERE id_stat=1">
[[getUni2Options]]
                    </select><br />
                    <div id="Labels"><label for="carrer_descp">Carrera:</label></div>
                    <input type="text" name="carrer_descp" id="carrer_descp" size="40" tabindex="1" eform="Carrera:string:1:Los numeros y simbolos no son aceptados:" /><br />
                    <hr />
                    <h4>Catedras</h4>
                    <table cellpadding="0" cellspacing="0" id="Catedras">
                        <tr>
                            <th><label for="id_mat[]">Código</label></th>
                            <th><label for="mat_basico[]">Básica</label></th>
                            <th><label for="mat_desc[]">Catedra</label></th>
                            <th><label for="mat_sem[]">Semestre</label></th>
                            <th><label for="mat_credit[]">Créditos</label></th>
                        </tr>
                        <tr id=\"Catedra\">
                            <td><input type="text" name="id_mat[]" id="id_mat" size="10" maxlength="10" tabindex="2" eform="Cod. Catedra:string:1:Solo numeros aceptados:" /></td>
                            <td><input type="checkbox" name="mat_basico[]" id="mat_basico" value="1" tabindex="3" /></td>
                            <td>
                                <input type="text" name="mat_desc[]" id="mat_desc" size="25" tabindex="4" eform="Catedra:string:1:Simbolos especiales no aceptados:" />
                            </td>
                            <td><input type="text" name="mat_sem[]" id="mat_sem" size="5" maxlength="2" tabindex="5" eform="Semestre:string:1:Letras y simbolos no son aceptados:" /></td>
                            <td><input type="text" name="mat_credit[]" id="mat_credit" size="5" maxlength="2" tabindex="6" eform="Creditos:string:1:Letras y simbolos no son aceptados:" /></td>
                        </tr>
                    </table>
                </fieldset>
                <span><a href="#" onclick="return false;" id="otraCatedra">Añadir Catedra</a></span>
                <input name="Reset" type="reset" value="Cancelar" />
                <input name="Submit" type="submit" value="Aceptar" />
            </form>

#186: 2-Feb-2010, 08:30 AM

rolph
Posts: 43

Hi,

I've just installed this snippet and I get this error:


Fatal error: Call to undefined function: eform2db() in E:\hshome\arterial\arterial.com.au\microsite\assets\snippets\eform\eform.inc.php on line 386

Does anyone know how to fix this?

Thanks!


Sarah

Hello,
have you resolve this problem?

I ask, because i've the same problem too.

#187: 2-Feb-2010, 03:07 PM

Coding Team

sottwell
Posts: 10,515

WWW
The snippet containing the eForm2db function has to be called before the eForm snippet is called.
sottwell.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html

#188: 4-Feb-2010, 03:52 PM

Coding Team

pixelchutes
Posts: 886

WWW
The snippet containing the eForm2db function has to be called before the eForm snippet is called.

Also, with the newer versions of eForm, you can just use eForm's runSnippet option:

Code:
[!eform2db_snippet!]
[!eForm? &eFormOnBeforeMailSent=`eform2db` ... !]
would become:
Code:
[!eForm? &runSnippet=`eform2db_snippet` &eFormOnBeforeMailSent=`eform2db` ... !]

As sottwell mentioned, this method will ensure that the snippet containing the eForm2db function is called in the correct order.
Mike Reid - www.pixelchutes.com
MODx Team Member / Contributor
[Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
________________________________
Where every pixel matters.

#189: 19-Feb-2010, 10:10 AM

svarga
Posts: 16

May be I'm missing something, but are BOTH actions - (1)writing in database and (2)send notification by email is activated in this snippet?

Second question - In my form I need file upload field. Is it possible in eForm2db? If no, how to integrate this two function?
Thank you.

#190: 19-Feb-2010, 11:55 AM

presson83
Posts: 77

I've got checkboxes in my form. When it saves the form data into the DB it just prints the word "ARRAY". How do you save the array of info from a form with checkboxes?
NICK HOAG
Web Developer
ETERNITY WEB - http://eternitywebdev.com

#191: 22-Feb-2010, 07:22 PM

Coding Team

pixelchutes
Posts: 886

WWW
I've got checkboxes in my form. When it saves the form data into the DB it just prints the word "ARRAY". How do you save the array of info from a form with checkboxes?

Try changing the name of your checkboxes to include "[]" at the end.

E.g.
name="colors[]" value="Red"
name="colors[]" value="Blue"
name="colors[]" value="Green"
Mike Reid - www.pixelchutes.com
MODx Team Member / Contributor
[Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
________________________________
Where every pixel matters.

#192: 26-Feb-2010, 09:32 AM

itough
Posts: 19

I am sorry to revisit this post with a question but I have spent about 10 hours struggling with what I suspect is a really simple issue. I am very new to PHP and SQL.
I am using MODx 1.0.2, eForm 1.4.4.6 and eForm2db 0.1 Beta 1.
I can get eForm2db to place data into my MODx database using the code quoted below. However, I wish to put the data into another database. The other database is on the same server and has the same user name and password as the MODx database.
The MODx database is called: william_modx
The table is called: modx_customers
The alternative database is called: william_data
the alternative table in this database is called: modx_customers
Code:
<?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
$dbTable['name'] = $fields['name'];
$dbTable['name2'] = $fields['name2'];
$dbTable['email'] = $fields['email'];
$dbTable['comments'] = $fields['comments'];
$dbTable['mailing'] = $fields['mailing'];

// Insert field/value pairs to insert/update in our table
//$dbTable[name] = $fields[first_name].' '.$fields[last_name]; // Merge two form fields together
//$dbTable[datetime] = date( 'YmdHis', strtotime( $fields[postdate] ) ); // Massage the postdate timestamp to be MySQL insert friendly


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

// Run the db insert query
$dbQuery $modx->db->insert$dbTable$table_prefix 'customers' );
return true;
}
// Return empty string
return $sql;
?>
Having looked at the DBAPI instructions it looks like I ougt to be able to insert
Code:
$ds = $modx->db->select('*','william_data.customers');
or
Code:
$ds = $modx->db->select('*','william_data.modx_customers');
to get this to put the data into the alternative database. However, I am not sure if this code is correct or exactly where to put it, or if there is an additional change I need to make. All experiments so far have resulted in errors or blank pages.
If anyone can help I would be very grateful. Thanks, Andy

#193: 26-Feb-2010, 01:33 PM

Coding Team

BobRay
Posts: 5,342

WWW
Having looked at the DBAPI instructions it looks like I ougt to be able to insert
Code:
$ds = $modx->db->select('*','william_data.customers');
or
Code:
$ds = $modx->db->select('*','william_data.modx_customers');
to get this to put the data into the alternative database. However, I am not sure if this code is correct or exactly where to put it, or if there is an additional change I need to make. All experiments so far have resulted in errors or blank pages.
If anyone can help I would be very grateful. Thanks, Andy


Did you try this:

Code:
$ds = $modx->db->select('*','william_modx.modx_customers');
and
Code:
$ds = $modx->db->select('*','william_data.modx_customers');

The part before the dot is the name of the DB, the part after it is the name of the table.
MODx info for newbies: http://bobsguides.com/MODx.html

#194: 26-Feb-2010, 02:27 PM

itough
Posts: 19

Hi Bob, Thanks for looking at this. I have often used your site when trying to expand my MODx abilities! In my post I mentioned that I have tried using:
Code:
$ds = $modx->db->select('*','william_data.customers');
OR
Code:
$ds = $modx->db->select('*','william_data.modx_customers');
You have asked if I have tried:
Code:
$ds = $modx->db->select('*','william_data.customers');
AND
Code:
$ds = $modx->db->select('*','william_data.modx_customers');
As I have tried them both already, Is my mistake that I should be using them both together?
Or am I simply placing them in the wrong part of the snippet? Where should this code go within the snippet?

Thanks, Andy

#195: 26-Feb-2010, 03:00 PM

Coding Team

BobRay
Posts: 5,342

WWW
Sorry, I think I misread your message. One or the other is correct -- I suspect the one with the full table name (including the prefix), but I'm not sure. I'm afraid the docs aren't explicit enough.

Also, I've been working in Revolution and am really rusty on the old way of doing DB access.

What you're trying to do should work, but the username and password need to be the same for the alternate table.


There's an example here that might help. If you can't get anywhere with your code, you might try the method here where you change the connection to the other DB with $modx->db->connect(credentials here), do the work, then change it back to the MODx DB with $modx->db->connect() with no arguments.

http://wiki.modxcms.com/index.php/API:DBAPI:connect

Check to make sure the connection was successful with this: http://wiki.modxcms.com/index.php/API:DBAPI:isConnected

Here's the full list of DBAPI functions: http://wiki.modxcms.com/index.php/API:DBAPI

As a last resort, you could try this and have two separate DB connections open at the same time: http://wiki.modxcms.com/index.php/API:DBAPI:config

Then you'd have two separate DB objects:

Code:
$modx->db   (the default DB)

$myDb = new DBAPI('12.23.4.5','mydb', 'username','password', 'myprefix_', '[charset]');

Then $modx->db->select() for one and $myDb->select() for the other.

Hope this helps.

Bob
MODx info for newbies: http://bobsguides.com/MODx.html

#196: 26-Feb-2010, 05:55 PM

itough
Posts: 19

Thanks for this Bob. I will follow up these leads later over the weekend. I'll let you know how I get on. It's good of you to take the time to help. Andy

#197: 28-Feb-2010, 05:24 PM

itough
Posts: 19

I think that previously I was putting the database select code in the wrong place.
Adding this does seem to get a connection to the database
Code:
// Bring needed resources into scope
$ds = $modx->db->select('*','william_data.modx_customers');
global $modx, $table_prefix;

However it results in this error
Code:
Fatal error: Call to a member function select() on a non-object in /home/william/www/dev/manager/includes/document.parser.class.inc.php(770) : eval()'d code on line 23

Line 23 of this document reads
Code:
        $this->loadedjscripts= array ();

It is in this context
Code:
// constructor
    function DocumentParser() {
        $this->loadExtension('DBAPI') or die('Could not load DBAPI class.'); // load DBAPI class
        $this->dbConfig= & $this->db->config; // alias for backward compatibility
        $this->jscripts= array ();
        $this->sjscripts= array ();
        $this->loadedjscripts= array ();
        // events
        $this->event= new SystemEvent();
        $this->Event= & $this->event; //alias for backward compatibility
        $this->pluginEvent= array ();
        // set track_errors ini variable
        @ ini_set("track_errors", "1"); // enable error tracking in $php_errormsg
    }
I don't know if this sheds any light on what I am doing wrong.
Perhaps I need to review why I am trying to do this? My concern is that I don't want anyone 'out there' to be able to use a feedback form to add content to my main MODx database. It seems more secure to use a separate database? Am I being unnecessarily paranoid?


#198: 28-Feb-2010, 08:47 PM

Coding Team

BobRay
Posts: 5,342

WWW
The document parser is just executing your code using eval() so line 23 is in your code (and the line number may not be accurate).

The error message is most likely saying that the $modx object is not available where you're calling select() (probably somewhere around line 23 of your code).

If you're doing that in a function, you need to have global $modx as the first line of the function. Sometimes that still doesn't work and you have to pass $modx as an argument to the function.

Feel free to post your complete snippet here if you're still stuck.
MODx info for newbies: http://bobsguides.com/MODx.html
Pages: 1 ... 8 9 [10]   Go Up
0 Members and 1 Guest are viewing this topic.