简体   繁体   中英

php insert multiple dynamic input data into database

I have this form data for images gallery and add dynamic input(add remove input field) using jquery for each new image:

<div class="row-1">
   <input type="hidden" name="list_item_image_large[]" value="">
   <div class="form-group col-md-12">
      <input type="text" class="form-control name="list_item_title[]" value="">
   </div>
   <div class="form-group col-md-12">
      <input type="text" class="form-control name="list_item_image_description[]" value="">
   </div>
   <div class="form-group col-md-12">
      <input type="text" class="form-control name="list_item_image_photographer[]" value="">
   </div>
   <div class="form-group col-md-12">
      <input type="text" class="form-control name="list_item_image_copyright[]" value="">
   </div>
</div>
<div class="row-2">
   <input type="hidden" name="list_item_image_large[]" value="">
   <div class="form-group col-md-12">
      <input type="text" class="form-control name="list_item_title[]" value="">
   </div>
   <div class="form-group col-md-12">
      <input type="text" class="form-control name="list_item_image_description[]" value="">
   </div>
   <div class="form-group col-md-12">
      <input type="text" class="form-control name="list_item_image_photographer[]" value="">
   </div>
   <div class="form-group col-md-12">
      <input type="text" class="form-control name="list_item_image_copyright[]" value="">
   </div>
</div>

now after submit i have this data:

Array
(
    [list_item_image_large] => Array
        (
            [0] => uploads/images/posts/1586075502_3460aaee4378986bf677.jpg
            [1] => uploads/images/2020/04/1585774613_c2f7ed8d8b0458271371.png
        )

    [list_item_title] => Array
        (
            [0] => 
            [1] => 
        )

    [list_item_image_description] => Array
        (
            [0] => 
            [1] => 
        )

    [list_item_image_photographer] => Array
        (
            [0] => 
            [1] => 
        )

    [list_item_image_copyright] => Array
        (
            [0] => 
            [1] => 
        )
)

now in action, i need to insert images data(title,description,photographer,copyright) for each image in mysql database. how do insert?!

The best way to do this would be to have a different structure.

Array
(
    [0] => Array
        (
            [list_item_title] => value
            [list_item_image_description] => value
            [list_item_image_photographer] => value
            [list_item_image_copyright] => value
        )

    [1] => Array
        (
            [list_item_title] => value
            [list_item_image_description] => value
            [list_item_image_photographer] => value
            [list_item_image_copyright] => value
        )
)

With this structure you could just simply use a for(each) loop

for($i=0; $i<count($array); $i++) {
  // mysql_stuff, can access field values like this: $array[$i]['list_item_title']
}

But this would require a different naming on the html input fields.

<input type="text" class="form-control name="image[0][list_item_title]" value="">
...
<input type="text" class="form-control name="image[1][list_item_title]" value="">
...

Meaning the logic of duplicating the fields would be a little different, but handling the data after the submit will become much easier.

Aggregate your data into individual items first. Something like this (fancier ways exist):

$items = [];

foreach($data['list_item_image_large'] as $num => $val) {
    $item[$num] = [
        'list_item_image_large' => $val,
        'list_item_title' => $data['list_item_title'][$num],
        'list_item_image_description' => $data['list_item_image_description'][$num],
        'list_item_image_photographer' => $data['list_item_image_photographer'][$num],
        'list_item_image_copyright' => $data['list_item_image_copyright'][$num]
    ];
}

var_dump($items); // See what you got there

You follow how the code above works, right? After that, it should be easy enough to insert the data into a database using your preferred method of interacting with MySQL. I assume you know how to insert data in general, and that you were simply struggling with the structure of your data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM