phparrays

Adding One Array Set with another to insert in database table


I have a form with some fixed input fields and some created dynamically from database questions table.

I want to insert data in database table forms_submissions

questions mysql table structure (Example) :

id  |  question                           |  question_column_name
 1  |  What is your Color of Choice ?     |   color_choice
 2  |  Your Favourite Dish ?              |   favourite_dish
 3  |  Your Favourite Plant ?             |   favourite_plant

Now in this table, many other questions will be added by site owner from admin dashboard provided. And a column with question_column_name will be added in forms_submissions database table while creating new question.

CURRENTLY I tried adding dynamic fields in another (3 rd) mysql table with this user's unique ID. But it is somewhat troublesome, when I try to make an option to export this all data to Excel with php from admin dashboard.

Now in front end, a form is created with some fixed input fields and other from this questions mysql table.

Form is like below :

<form> // here I entered shortest part of this tag
   <label>Your Full Name</label>
   <input type="text" name="full_name" class="form-control">
   
   <label>Your Email Id</label>
   <input type="text" name="email" class="form-control">

   <label>Your Mobile Number</label>
   <input type="tel" name="mobile" class="form-control">

   <?php
   $query = "select * from questions";
   $result = $database->get_results($query);
      foreach ($result as $data){
            $question = $data['question'];
            $question_column_name = $data['question_column_name'];
     ?>
   <label><?php echo $question;?></label>
    <input type="text" name="<?php echo $question_column_name;?>" class="form-control">

  <?php } ?>

   <input type="submit" name="mysubmit" value="Submit" class="btn btn-danger">

 </form>

Now when processing form submission, I am using following code to add data in database after proper sanitization.

<?php
if(isset($_POST['mysubmit'])){
    $insertdata = array(
      'full_name' => $full_name, // it is after proper sanitization done with filter function created in my mysqli wrapper class so then it is $full_name instead of $_POST['full_name']
      'email' => $email,
      'mobile' => $mobile,
   );
   $database->insert('forms_submissions', $insertdata);
 ?>

I tried :

 <?php
  // Adding after above $insertdata array code.....
  $query2 = "select * from questions";
  $result2 = $database->get_results($query2);
    $insertdata2= array();
    foreach ($result2 as $data2){
        $input_field = $data2['question_column_name'];
        $insertdata2 = array(
                $input_field => $_POST[$input_field],
            );
        }       
    /* I tried following array merging options */

    1) $insertdata_final = $insertdata + $insertdata2;
    2) $insertdata_final = array_merge($insertdata, $insertdata2);

     $database->insert('forms_submissions', $insertdata_final);

But Not Working above try... showing php errors regarding arrays.... (Warning: Array to string conversion)

I stuck how to add dynamically added input fields in this array ?

Is there any other way to add / merge arrays ??

EDIT - UPDATE :

with print_r($insertdata_final); I am getting following structure of arrays

Array ( 
    [0] => Array ( 
        [full_name] => XYZ 
   ) 
    [1] => Array ( 
       [email] => abc@gmail.com 
   ) 
    [2] => Array ( 
       [mobile] => 1234567890 
   ) 
 )



 Array (
    [3] => Array ( 
       [0] => Array ( 
         [color_choice] => Red 
       )
    ) 
    [4] => Array ( 
        [1] => Array ( 
        [favourite_dish] => Pizza
    ) 
   )
    [5] => Array ( 
        [2] => Array ( 
          [favourite_plant] => Hibiscus
        ) 
    )
 )

I am expecting Following Array Structure

 Array
  (
   [full_name] => XYZ
   [email] => abc@gmail.com
   [mobile] => 1234567890
   [color_choice] => ABC
   [favourite_dish] => DEF
   [favourite_plant] => HIJ
  )

Solution

  • You're overcomplicating it, actually. You can just write this:

    foreach ($result2 as $data2){
        $input_field = $data2['question_column_name'];
        $insertdata[$input_field] = $_POST[$input_field];
    }
    

    and then you'll get a flat structure like this:

    Array
    (
        [full_name] => XYZ
        [email] => abc@gmail.com
        [mobile] => 1234567890
        [color_choice] => ABC
        [favourite_dish] => DEF
        [favourite_plant] => HIJ
    )
    

    which will be easy to insert into your database.

    Live demo: https://3v4l.org/WrONl