 <p>It sounds like you were trying to used nested loops, but you in fact had one loop at the beginning which closed out and then you loop through the rest. You need to move your closing curly brace marked below:</p> <pre><code>for($i=0; $i&lt;count($_FILES['upload']['name']); $i++) { $filut = basename($_FILES['upload']['name'][$i]); } &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;=========== This curly brace closes the "for loop" if(isset($_POST['subi'])) { $data1 = $_POST['answers']; foreach($data1) as $postValues) { $sql4 = 'INSERT INTO db_image (text, image) VALUES("'.$postValues.'","'.$filut.'")'; $my-&gt;query($sql4); } } ??? &lt;&lt;&lt;&lt;&lt;======== For a nested loop you should close it here </code></pre> <p>or perhaps you were trying to assign <code>$filut</code> as an array in which case the starting <code>for...</code> loop needs to look like this:</p> <pre><code>for($i=0; $i&lt;count($_FILES['upload']['name']); $i++) { $filut[] = basename($_FILES['upload']['name'][$i]); } </code></pre> <p>(Note the addition of <code>[]</code> after <code>$filut</code>.)</p> <p>However, if this is what you were intending (to make $filut into an array) then you need to do something different down below in order to access just one element of the array. This is where a nested <code>foreach</code> would solve the problem - maybe this is how you intended to finish it off:</p> <pre><code>if(isset($_POST['subi'])) { $data1 = $_POST['answers']; foreach($data1 as $postValues) { foreach ($filut as $f) { $sql4 = 'INSERT INTO db_image (text, image) VALUES("'.$postValues.'","'.$f.'")'; $my-&gt;query($sql4); } } } </code></pre> <p>This will insert a row for every value in $filut for every value in $_POST['answers']. Is that what you wanted? Or are $filut and $_POST['answers'] in parallel and you want a 1-1 correlation on the insert?</p> <p>Finally I'm going to suggest that you use prepare and bind since you are getting your data from the user. This example copied/modified from <a href="http://php.net/manual/en/mysqli-stmt.execute.php" rel="nofollow">here</a>:</p> <pre><code>&lt;?php $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } /* Prepare an insert statement */ $query = "INSERT INTO db_image (text, image) VALUES (?,?)"; $stmt = $my-&gt;prepare($query); #### NOTE THE USE OF YOUR VARIABLES HERE IN THE BIND - the "ss" means they are strings $stmt-&gt;bind_param("ss", $postValues, $f); #### HERE IS WHERE YOUR CODE IS GETTING INSERTED foreach($data1 as $postValues) { foreach ($filut as $f) { /* Execute the statement */ $stmt-&gt;execute(); } } /* close statement */ $stmt-&gt;close(); /* close connection */ $my-&gt;close(); ?&gt; </code></pre> <p>I can't guarantee that I got all the variables changed to match your code, but hopefully it's close enough to give you an idea. This solution (1) protects you from SQL Injection (which is a serious flaw in your code otherwise) and (2) will be much faster by means of the prepare/execute cycle.</p> <p><em>Do note the careful indentation in my examples where everything lines up perfectly with 3 or more spaces per indent - I can't tell you how many times I have seen sloppy (or too little) indentation make people miss problems which would be otherwise obvious.</em></p>