Jump to content

Dynamic Multiple Textareas to a Dynamic MYSQL Insert ( Sorry long post!!)


Shabalaka

Recommended Posts

First off i would like to say thanks to all who read this post fully.

 

Hi Guys i been trying to do this for days but i cant seem to work it out, So im posting here in the hope that someone can help me.

 

What i have is 3 forms with all different amounts of textareas to paste content into and each line of content will correspond to the next ones to create one row of data per line in each textarea.

 

I know i have to use explode function to break all the data down by the delimiter which i have done.

 

The PHP Code i have at the moment is this.

 

// I,m passing all the POST vars because ideally i would like this function to be dynamic and build the SQL statement from all the field names , No MYSQL injection protection during testing  plan to add later 
function dataformat($_POST){ 
         // This example will receive the following variables. 
( [0] => title [1] => fname [2] => lname [3] => email [4] => submit ) 
these field names match the db table names.
        // The KEYS SENT BY POST IE THE FIELD NAMES. 
        $columns = array(); 
        // The COrresponding VALUES FOR THE FIELDS. 
        $rows = array(); 
        $i = 0; 

            foreach ($_POST as $key=>$value){ 
                //Push the field name into the keys array 
                array_push($columns,$key); 
                array_push($rows,$value); 
            }     
             
            foreach($rows as $keys=>$values){ 
                //create the array values 
                $values = explode("\r\n",$values); 
                echo "<br>"; 
                print_r($values);     
                echo "<br>"; 
                echo "<br>"; 
                echo "Columns : ".count($columns)."<Br>"; 
                echo "Values : ".count($values)."<br>"; 
                echo "Rows : ".count($rows[1])."<br>"; 
                echo "<br>"; 
                echo $columns; 
                 
            } 
            // Pop the 2 submits off of rows and columns. 
                array_pop($columns); 
                array_pop($rows); 
                 
                 
                 
    } 

 

TThe output from this code looks like this

 

Array ( [0] => Mr [1] => Miss [2] => Mr )

 

Columns : 5

Values : 3

Rows : 1

 

Array

Array ( [0] => Test [1] => Alice [2] => Joe )

 

Columns : 5

Values : 3

Rows : 1

 

Array

Array ( [0] => Admin [1] => Wonderland [2] => Bloggs )

 

Columns : 5

Values : 3

Rows : 1

 

Array

Array ( [0] => test@test.com [1] => alice@wonderland.com [2] => joe@bloggs.com )

 

Columns : 5

Values : 3

Rows : 1

 

Array

Array ( [0] => Submit )

 

Columns : 5

Values : 1

Rows : 1

 

Array

 

Now as you can see all the details for one user is in each array value ie..

 

Mr Test Admin has the Email test@test.com inside the [0] elements of each array.

and [1] is a new user

and element 2 is another users name and emails.

 

Ideally what i would like to achieve is a looped dynamic SQL which will take these elements and build an SQL statement to input them as seperate rows in the database.

 

Any help would be appreciated thanks

 

Regards Shab

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.