Simultaneously Insert $_POST and $_FILES data In mysql

Viewed 32

Hi I have a confusion and I am not able to do this task.

I wanted to insert text from $_POST and images from $_FILES simultaneously in mysql db In following order.

TEXT (1)
IMAGE (2)
TEXT (3)
TEXT (4)
IMAGE (5)

For getting this output I named every input as 1,2,3, and so on for getting considered in an order.

<input type="text" name="1">
<input type="file" name="2">
<input type="text" name="3">
<input type="text" name="4">
<input type="file" name="5">

Now I have applied loop to manually encounter each input. But I think that's useless because I can only apply loop on either $_POST or $_FILES at a time. And ultimately I get output as

//If I consider POST DATA first

TEXT (1)
TEXT (3)
TEXT (4)
IMAGE (2)
IMAGE (5)



//If I consider FILES DATA first
IMAGE (2)
IMAGE (5)
TEXT (1)
TEXT (2)
TEXT(4)

/

how do I perform this task? Can Anyone help?

2 Answers

First of all, upload your files to the destination folder using PHP native function move_uploaded_file() and store the name of each file in an array & then store your text and images name in your Database.

<?php
    if(isset($_POST)){
      $data = $_POST;
      $files = $_FILES;
      $images_name = array();
      //loop through the $files array and store them in your destination folder
      //and store name of each file in $images_name;
      //then store your data and files name in the database
    }
?>

You can try this :

$data = $_POST + $_FILES;
ksort($data);

It will merge arrays preserving keys and then sort the resulting array by keys.

Related