php - foreach loop create a duplicated of last iteration -


so have php foreach loop inserts many images upload in directory , creates row in db it. works great except duplicating last iteration.

i have tried doing array_splice didn't work.

i know query subject sql injections , fix once iteration duplication sorted.

$errors= array(); foreach($_files['userfile']['tmp_name'] $key => $tmp_name ){     $file_name = time().$_files['userfile']['name'][$key];     $file_size =$_files['userfile']['size'][$key];     $file_tmp =$_files['userfile']['tmp_name'][$key];     $file_type=$_files['userfile']['type'][$key];        if($file_size > 2097152){         $errors[]='file size must less 2 mb';     }            $query="insert table (user_id, filename) values ('$user_id', '$file_name')";     $desired_dir="/items/";     if(empty($errors)==true){         if(is_dir($desired_dir)==false){             mkdir("$desired_dir", 0700);        // create directory if not exist         }         if(is_dir("$desired_dir/".$file_name)==false){             move_uploaded_file($file_tmp,"/items/".$file_name);         }else{                                  //rename file if 1 exist             $new_dir="/items/".$file_name.time();              rename($file_tmp,$new_dir) ;                        }         mysql_query($query);                 }else{             print_r($errors);     } } if(empty($error)){     echo "success"; } if ($conn->query($query) === true) { echo "done"; } else { echo "error: " . $query . "<br>" . $conn->error; } 

any ideas why doing this?

thanks in advance!

inside last if.. else.. section you're calling $conn->query($query). run whatever in $query - in case last value set inside foreach loop. that's what's causing duplicate last entry.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -