#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Need to store multiple array from form to mysql


    I have an html form that, via javascript, depending on the user selection, appears some input fields in a row. This is the javascript:
    Code:
    function addInputAdult(num){
    inputElementsAdult.innerHTML='';
    for(xAdult=0;xAdult<num;xAdult++)inputElementsAdult.innerHTML=inputElementsAdult.innerHTML +'<table width="100%" style="margin:0; padding:0;"><tr><th width="24%" scope="col">Adult '+(xAdult + 1)+' - Name</th><th width="13%" scope="col">Birthdate</th><th width="18%" scope="col">Place of Birth</th><th width="13%" scope="col">Passport Nr</th><th width="13%" scope="col">Issue Date</th><th width="16%" scope="col">Expiry Date</th></tr><tr><td> <input name="adlt[][Emri]" type="text" id="adltEmri" style="width:98%;" /></td><td> <input style="width:95%;" type="text" name="adlt[][Dtlindja]" id="adltDtlindja" /></td><td> <input style="width:90%;" type="text" name="adlt[][Vendlindja]" id="adltVendlindja" /></td><td> <input style="width:90%;" type="text" name="adlt[][Pass]" id="adltPass" /></td><td> <input style="width:90%;" type="text" name="adlt[][DtLeshimit]" id="adltDtLeshimit" /></td><td><input style="width:90%;" type="text" name="adlt[][DtSkadimit]" id="adltDtSkadimit" /></td></tr></table>';
    }
    So, the user make the selection and the form appears. Now, i want to add the user input details in my mysql database. Can you help me on this please? I'm not "sure" how to do this.

    My problem is on getting the informations from the form in a way that those information could be usable and understandable. What i have tried is:


    PHP Code:
    foreach ( $_POST['adlt'] as $adult) {
    $getd=implode(",",$adult);
    echo 
    $getd;


    ...but it shows all the information not separated and I don't know how to separate informations from each other. Output looks like this:
    Code:
    adult1 Nameadult1Birthdateadult1Passportadult1PlaceofBirthadult1IssueDateadult1expirydateadult2 Nameadult2 Birthdate...and so on
    Can you help me on this, please?

    Thank you in advance
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    I don't suppose you could provide a nicer layout of your form? And as far as the arrays, do you wish the items of your form not be their own items? Usually someone may do something like this.

    Code:
    <FORM type="post" action="proc.php">
      <INPUT type="text" name="name" /><br />
      <INPUT type="text" name="age" /><br />
      <INPUT type="radio" name="sex" value="male" /><br />
      <INPUT type="radio" name="sex" value="female" /><br />
    </FORM>
    PHP Code:
    <?php
      $statement 
    $link->prepare("INSERT INTO `tablename` (namecol,agecol,sexcol) VALUES (?,?,?)");
      
    $statement->bind_param('sis'$_POST['name'], $_POST['age'], $_POST['sex']);
      
    $statement->execute();
      
    $statement->close();
    ?>
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0
    Originally Posted by Triple_Nothing
    I don't suppose you could provide a nicer layout of your form? And as far as the arrays, do you wish the items of your form not be their own items? Usually someone may do something like this.

    Code:
    <FORM type="post" action="proc.php">
      <INPUT type="text" name="name" /><br />
      <INPUT type="text" name="age" /><br />
      <INPUT type="radio" name="sex" value="male" /><br />
      <INPUT type="radio" name="sex" value="female" /><br />
    </FORM>
    PHP Code:
    <?php
      $statement 
    $link->prepare("INSERT INTO `tablename` (namecol,agecol,sexcol) VALUES (?,?,?)");
      
    $statement->bind_param('sis'$_POST['name'], $_POST['age'], $_POST['sex']);
      
    $statement->execute();
      
    $statement->close();
    ?>
    Hello,
    First of all i want to thank you for the reply. Unfortunately i can't do a simple form since the user should select how many persons will be, and depending on the selections, the javascript should show the input fields that are the same for each person and the user should complete those fields with necessary informations.
    Thanks!
  6. #4
  7. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    why not try this

    PHP Code:
    $getd '';

    foreach ( 
    $_POST['adlt'] as $adult) {

    $getd .= $adult.', ';

    }  

    echo 
    $getd;

    // OR

    $getd = array();

    foreach ( 
    $_POST['adlt'] as $adult) {

    $getd[] = $adult;

    }  

    echo 
    implode(',' $getd); 
    Just wondering though, you're not seriously storing these "arrays" in your database as such right? Because that is probably the worst approach.
    Last edited by aeternus; February 23rd, 2013 at 05:37 PM.
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    why do you need a readable output format when you want to store the data in the database? I don't see the connection between those two things.

    Anyway, the core issue and the reason why your implode() doesn't really do anything is that you're not using a sensible array structure.

    Currently, you have all fields in a single big numerically indexed array, and each field is represented by an associative array consisting of a single element (the value of the field):

    PHP Code:
    [
        [
    'field_1' => 'val_11']        // first row starts here
        
    ['field_2' => 'val_12'],
        ...
        [
    'field_n' => 'val_1n'],    // first row ends here
        
    ['field_1' => 'val_21'],    // second row starts here
        
    ['field_2' => 'val_22'],
        ...
        [
    'field_n' => 'val_2n'],    // second row ends here
        
    ...
        [
    'field_n' => 'val_m1'],
        [
    'field_2' => 'val_m2'],    // m-th row starts here
        
    ...
        [
    'field_n' => 'val_mn']        // m-th row ends here

    This makes it very hard to actually group the values per user. You'd basically have to hard-code the number of fields per row and then loop over array slices, plus you'd have to somehow extract those associative arrays to assign the values.

    So this is obviously a bad structure. Instead, use explicit indices to create an array of associative arrays, each one representing a single row:

    Code:
    name="adlt[' + xAdult + '][Emri]"
    Do that for each field. Then you'll get this structure:

    PHP Code:
    [
        [    
    // first row
            
    'field_1' => 'val_11',
            
    'field_2' => 'val_12',
            ...
            
    'field_n' => 'val_1n'    
        
    ],
        [    
    // second row
            
    'field_1' => 'val_21',
            
    'field_2' => 'val_22',
            ...
            
    'field_n' => 'val_2n'    
        
    ],
        ...
        [    
    // m-th row
            
    'field_1' => 'val_m1',
            
    'field_2' => 'val_m2',
            ...
            
    'field_n' => 'val_mn'    
        
    ]

    And that's much easier to handle. To insert all rows, you'd simply have to loop over the array elements and access the specific fields in each one.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0
    Originally Posted by Jacques1
    Hi,

    why do you need a readable output format when you want to store the data in the database? I don't see the connection between those two things.

    Anyway, the core issue and the reason why your implode() doesn't really do anything is that you're not using a sensible array structure.

    Currently, you have all fields in a single big numerically indexed array, and each field is represented by an associative array consisting of a single element (the value of the field):

    PHP Code:
    [
        [
    'field_1' => 'val_11']        // first row starts here
        
    ['field_2' => 'val_12'],
        ...
        [
    'field_n' => 'val_1n'],    // first row ends here
        
    ['field_1' => 'val_21'],    // second row starts here
        
    ['field_2' => 'val_22'],
        ...
        [
    'field_n' => 'val_2n'],    // second row ends here
        
    ...
        [
    'field_n' => 'val_m1'],
        [
    'field_2' => 'val_m2'],    // m-th row starts here
        
    ...
        [
    'field_n' => 'val_mn']        // m-th row ends here

    This makes it very hard to actually group the values per user. You'd basically have to hard-code the number of fields per row and then loop over array slices, plus you'd have to somehow extract those associative arrays to assign the values.

    So this is obviously a bad structure. Instead, use explicit indices to create an array of associative arrays, each one representing a single row:

    Code:
    name="adlt[' + xAdult + '][Emri]"
    Do that for each field. Then you'll get this structure:

    PHP Code:
    [
        [    
    // first row
            
    'field_1' => 'val_11',
            
    'field_2' => 'val_12',
            ...
            
    'field_n' => 'val_1n'    
        
    ],
        [    
    // second row
            
    'field_1' => 'val_21',
            
    'field_2' => 'val_22',
            ...
            
    'field_n' => 'val_2n'    
        
    ],
        ...
        [    
    // m-th row
            
    'field_1' => 'val_m1',
            
    'field_2' => 'val_m2',
            ...
            
    'field_n' => 'val_mn'    
        
    ]

    And that's much easier to handle. To insert all rows, you'd simply have to loop over the array elements and access the specific fields in each one.
    Hello,
    Thank you very much for your help. Now the array is structured as follow:

    Code:
    [adlt] => Array
            (
                [0] => Array
                    (
                        [Emri] => Adult 1
                        [Dtlindja] => BirthDate
                        [Vendlindja] => PlaceofBirth
                        [Pass] => BB123654789
                        [DtLeshimit] => 22.02.2012
                        [DtSkadimit] => 21.02.2013
                    )
    
                [1] => Array
                    (
                        [Emri] => Adult 2
                        [Dtlindja] => Birthdate 2
                        [Vendlindja] => PlaceofBirth 2
                        [Pass] => CC987456321
                        [DtLeshimit] =>02.01.2012
                        [DtSkadimit] => 01.01.2012
                    )
    
            )
    This is a big step ;-) Now, how sql query should look like for inserting those informations? Can you help me on this, too, pleease?
    Thanks again!
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Please don't full-quote the post directly above you, especially not when it's long.

    The INSERT query works just like every other INSERT query:
    Code:
    INSERT INTO
    	your_table (col_1, col_2, ..., col_n)
    VALUES 
    (val_11, val_12, ..., val_1n),
    (val_21, val_22, ..., val_2n),
    ...
    (val_m1, val_m2, ..., val_mn)
    ;
    And the tuples are of course constructed from the "rows" in $_POST.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo