0

I have a form wherein each input is contained within array items[]. This array contains both text inputs and file inputs. If that's already a huge red flag, then TLDR - can an array containing both text and file inputs be passed to the same SQL statement for database upload?

I have an echo statement in my PHP where I try to echo the names of the inputs, "paragraph" and "image". "paragraph" is a text input, and "image" is a file input.

foreach($_POST['items'] as $index => $item){

    $key = key($item);
    echo $key;
    ....

^ This echoes "paragraph", but does not echo "image".

For a bit more clarity, here is the form:

<form method="post" action="insert.php" enctype="multipart/form-data">

    <textarea name="title"></textarea>

    <!--paragraph input-->
    <div><textarea name="items[][paragraph]"></textarea></div>
    <!--paragraph input-->
    <div><textarea name="items[][paragraph]"></textarea></div>
    <!--paragraph input-->
    <div><textarea name="items[][paragraph]"></textarea></div>
    <!--file input-->
    <div><input type="file" name="items[][image]" id="uploadImage" multiple></div>
    <!--file input-->
    <div><input type="file" name="items[][image]" id="uploadImage" multiple></div>
    <!--file input-->
    <div><input type="file" name="items[][image]" id="uploadImage" multiple></div>

    <input type="submit" name="upload" value="Upload" id="upload">

</form>

...and the php. There is no handling of the file type and storing it in a directory yet since I'm just trying to see if it's possible for the foreach to recognize the file input.

<?php

    if (isset($_POST['upload'])) {

    $host = "";
    $dbname = "";
    $user = "";
    $pass = "";

    try {    
        $db = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $statement = $db->prepare("INSERT INTO table (placement, datatype, typetext) VALUES (:placement, :datatype, :typetext)");

        if($statement){

            if(is_array($_POST['items'])){

                foreach($_POST['items'] as $index => $item){

                    //this is where I'm trying to catch the names of
                    //the inputs, "paragraph" and "image". This will
                    // print "paragraph", but will NOT print "image"

                    $key = key($item);
                    echo $key;

                    $statement->execute(['placement' => $index, 'datatype' => key($item), 'typetext' => $item[key($item)]]);
                }
            }
        }
        $db = null;
    }
    catch(PDOException $e) {
        echo $e->getMessage();
    }
    exit(header("location: insert.php"));
}
?>

I'm guessing that, because the foreach contains $_POST['items'] and not something like $_FILES['items'], this will not work. Is that the case? The main reason I have everything inside of the array items[] is so that I can store an index of these items within the database, so that when I retrieve the contents of a single post, I can place them in the correct order. If I'm going about this the wrong way, what is a better way to do this?

rpivovar
  • 3,150
  • 13
  • 41
  • 79
  • Also, my apologies for the shoddiness of my previous question, since I didn't supply enough information and edited it to hell (though I did learn a lot from the comments and answers). I hope this question is a little more clear. – rpivovar Jul 15 '17 at 03:27
  • If something in your $items array is another array, you're going to have to iterate over that array as well. – Difster Jul 15 '17 at 03:29
  • 1
    you can loop post and files and or glue them together, the question is not clear. –  Jul 15 '17 at 03:29
  • 1
    What do you think this is `C#` ? lol – ArtisticPhoenix Jul 15 '17 at 03:30
  • 1
    I don't know why you're using multi-dimensional arrays to start with. If the goal as you say is to store an index of the different values of the files, then read http://php.net/manual/en/features.file-upload.post-method.php and you can use different variables for the different options that files offers, given "if" that's what the question is about. Or `foreach` on each input minus the extra arrays. – Funk Forty Niner Jul 15 '17 at 03:36
  • @rtfm I guess I'm really trying to figure out if it's possible to have a form with multiple inputs, loop through those inputs and find the ones that are of file type, and then do something differently with those (ie store the files in a directory and get the name of the file). Once I can get the name of the file, then it can be stored in the database in the same way as the paragraph - index in the form (`'placement'`), the type of input ie paragraph or file (`'datatype'`), and the text contained therein for that input (`'typetext'`) – rpivovar Jul 15 '17 at 03:36
  • @Fred-ii- I have a working copy of this whole thing that is NOT a multi-dimensional array, but I couldn't figure out how to get the index of each individual input in the form. Do you think I should avoid the multi-dimensional approach? – rpivovar Jul 15 '17 at 03:38
  • well the file type ones are in `$_FILES` –  Jul 15 '17 at 03:38
  • 1
    @coffeebot *"Do you think I should avoid the multi-dimensional approach?"* - Indeed you should and it is causing some major headaches now and will cause you "migraines" later ;-) – Funk Forty Niner Jul 15 '17 at 03:39
  • This is how I was doing it pre-multi array : https://stackoverflow.com/questions/45091567/php-get-index-of-element-with-tag-name , though I'm still not sure how to get the index of each element in a dynamic form like that. – rpivovar Jul 15 '17 at 03:41
  • @Fred-ii- I will take your advice and go that route and try to figure out how to get the indexes – rpivovar Jul 15 '17 at 03:43
  • Cool, wishing you well :) cheers – Funk Forty Niner Jul 15 '17 at 03:47

1 Answers1

1

you can access file inputs using $_FILES but not the content available variables are name, type, tmp_name, error and size

foreach($_FILES['items'] as $index => $item){
  foreach ($item as $value) {
    echo "$index : {$value['image']} <br>";
  }
}
ewwink
  • 18,382
  • 2
  • 44
  • 54