Back to Home: PHP FormMail Generator I Need Help : Ask Question Without Registration

How to save form data to MySQL database?
  • admin October 2011

    You need to make up sql query based on the form field/value pairs. You can probably do something like this in form.lib.php :

    1) manually add "dbfield" name to map the form field name with mysql table field

    2) get the query read of name/value pairs

    $GLOBALS['form_mail'] = array();
    $GLOBALS['form_mail']['field_0'] = array( "name" => "field_0", "text" => "smtp",  "type" => "", "instruction" => "", "required" => "" ) ;
    $GLOBALS['form_mail']['field_1'] = array( "name" => "field_1", "dbfield" => "email", "text" => "Email",  "type" => "sender's email", "instruction" => "Your email address", "required" => "Required" ) ;
    $GLOBALS['form_mail']['field_2'] = array( "name" => "field_2", "dbfield" => "title", "text" => "Title",  "type" => "titleofsender", "instruction" => "Your title", "required" => "Required" ) ;
    $GLOBALS['form_mail']['field_3'] = array( "name" => "field_3", "text" => "Name",  "type" => "sender's name", "instruction" => "", "required" => "Required" ) ;
    $GLOBALS['form_mail']['field_4'] = array( "name" => "field_4", "text" => "Section Break Text Goes Here",  "type" => "sectionbreak", "instruction" => "", "required" => "Not Required" ) ;
    $GLOBALS['form_mail']['field_5'] = array( "name" => "field_5", "text" => "Generic Email",  "type" => "generic email", "instruction" => "", "required" => "" ) ;

    ....

    function phpfmg_getSQL(){
        $dbFields = array();
        foreach( $GLOBALS['form_mail'] as $field ){
            if( isset($field["dbfield"]) ){
                $value = phpfmg_field_value( $field[ "name" ] );
                $dbFields[] = "`{$field['dbfield']}` = \"" . mysql_escape_string( $value ) . "\"";
            };
        }
        return join(",", $dbFields);
    }

    3) after the mail sent, use the above function phpfmg_getSQL() to get the query string to make up a complete insert query, then execute the query to insert the form data to MySQL