admin管理员组

文章数量:1123345

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] => [email protected] 
   ) 
    [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] => [email protected]
   [mobile] => 1234567890
   [color_choice] => ABC
   [favourite_dish] => DEF
   [favourite_plant] => HIJ
  )

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] => [email protected] 
   ) 
    [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] => [email protected]
   [mobile] => 1234567890
   [color_choice] => ABC
   [favourite_dish] => DEF
   [favourite_plant] => HIJ
  )
Share Improve this question edited 11 hours ago Dr M L M J asked 12 hours ago Dr M L M JDr M L M J 2,3975 gold badges19 silver badges40 bronze badges 11
  • The issue is in how you're merging arrays for dynamic fields. In your current approach, you're overwriting $insertdata2 in every loop iteration, which results in only the last value being retained. Instead of overwriting $insertdata2 in every loop iteration, try adding dynamic fields to the $dynamicData array. – TSCAmerica.com Commented 11 hours ago
  • Have you verified that $insertdata and $insertdata2 contain what you expect? Try doing a var_dump($insertdata, $insertdata2) just before your merge to check what they contain. – Rob Eyre Commented 11 hours ago
  • // it is after proper sanitization done with filter function... I dread to think what this means. Please read How to include a PHP variable inside a MySQL statement and ensure you're actually following the correct, secure, reliable process of using prepared statements and parameters to build your SQL queries, and not relying on some cargo-cult nonsense which does not provide proper protection against injection attacks etc. – ADyson Commented 11 hours ago
  • If you're getting an error message, tell us exactly what the error was, and point out which line PHP told you threw the error. Otherwise we're having to guess. – ADyson Commented 11 hours ago
  • 1 @ADyson Edited question.... – Dr M L M J Commented 11 hours ago
 |  Show 6 more comments

1 Answer 1

Reset to default 1

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] => [email protected]
    [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

本文标签: phpAdding One Array Set with another to insert in database tableStack Overflow