Page 1 of 1

Array into variables - Stuck for a few hours now

Posted: Sun Oct 24, 2010 11:45 am
by synical21
Hey been awhile since I posted here, I am using a contact form script on a wordpress site but I have changed it to submit data to the database instead of mail.

The way the scripts handles the posted data is confusing but this is how it does it originall before editing:

Code: Select all

  public function saveFormData($cf7) {
        global $wpdb;
        $time = $_SERVER['REQUEST_TIME'] ? $_SERVER['REQUEST_TIME'] : time();
        $ip = ($_SERVER['X_FORWARDED_FOR']) ? $_SERVER['X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
        $tableName = $this->prefixTableName('SUBMITS');
        $parametrizedQuery = "INSERT INTO `$tableName` (`submit_time`, `form_name`, `field_name`, `field_value`) VALUES (%s, %s, %s, %s)";

        $title = $this->stripSlashes($cf7->title);
        foreach ($cf7->posted_data as $name => $value) {
            $value = is_array($value) ? implode($value, ", ") : $value;
            $wpdb->query($wpdb->prepare($parametrizedQuery,
                                        $time,
                                        $title,
                                        $this->stripSlashes($name),
                                        $this->stripSlashes($value)));
        }
This creates a record per field of the form, so if i have a 20 field form there will be 20 database records. I cant use this I need 1 record per form submit. This is what I have tried to do but it is clearly wrong:

Code: Select all

foreach ($cf7->posted_data as $name => $value) {
extract($value);
}

 $parametrizedQuery = mysql_query("INSERT INTO `$tableName` (`submit_time`, `form_name`, `field_name`, `field_value`) VALUES ('$time', '$yourname', '$youremail', '$yourmessage')");
the names of the contact fields are yourname/youremail/yourmessage so i tried using extract to sperate each name and the value to a variable. That is what I understood from google but im now lost as every record is now blank so the variables clearly have no values.

Please help :banghead:

Re: Array into variables - Stuck for a few hours now

Posted: Sun Oct 24, 2010 4:08 pm
by twinedev
First and foremost. Will this script be called from a particular form where the field names/count will never change?

if this is the case, then I can help ya rewrite it, however if this will be called from several forms, that may have different fields, you will want to keep it the original way it is and then just write a better script to retrieve the data.

-Greg

Re: Array into variables - Stuck for a few hours now

Posted: Sun Oct 24, 2010 4:11 pm
by synical21
The field names/count will not change, the form will just be duplicated on different pages of the site.