James Allan
James Allan

Reputation: 280

Uploading multiple images one to each row PHP and Mysqli db

I have 3 images to upload in my form.

    <form action="" method="post" enctype="multipart/form-data" class="form-horizontal">
   <div class="form-group col-md-5">
     <label for="image">Centralizada</label>
       <input id="image" type="file" name="image" class="btn btn-danger">
     </div>

    <div class="form-group col-md-5">
     <label for="img_v1">V1</label>
       <input id="img_v1" type="file" name="img_v1" class="btn btn-danger">
    </div>

   <div class="form-group col-md-5">
      <label for="img_v2">V2</label>
        <input id="img_v2" type="file" name="img_v2" class="btn btn-danger">
   </div>.

And i have these three rows in my database: image, img_v1, img_v2

And i want to upload all the 3 images to each row but my script is not working. Its posting only the first image and the other two is not.

this is my script

<?php 
include("includes/dbconn.php");


    $error = ''; 
    if(isset($_POST['submit_post'])){
    $title = strip_tags($_POST['title']);
     $date = date('Y-m-d h:i:s');
    if($_FILES['image']['name'] !=''){
    $image_name = $_FILES['image']['name']; 
    $image_tmp = $_FILES['image']['tmp_name'];
    $image_size = $_FILES['image']['size'];
    $image_ext = pathinfo($image_name,PATHINFO_EXTENSION);
     $image_path = '../clientes/img/'.$image_name;
    $image_db_path = 'img/'.$image_name;
    if($image_size < 10000000){
    if($image_ext == 'jpg' || $image_ext == 'png' || $image_ext == 'jpeg' ||     $image_ext == 'gif'){
   if(move_uploaded_file($image_tmp,$image_path)){
  $ins_sql = "INSERT INTO gallery (title, description, image, img_v1,  img_v2, category, status) VALUES ('$title', '$_POST[description]',
   '$image_db_path', '$image_db_path', '$image_db_path', '$_POST[category]',   '$_POST[status]')";

Why my other images is now uploading? Obs:im learning php -noob

Upvotes: 1

Views: 560

Answers (1)

cmnardi
cmnardi

Reputation: 1111

Try this (I have not tested)

function imageUpload($field)
{
    $image_name = $_FILES[$field]['name'];
    $image_tmp = $_FILES[$field]['tmp_name'];
    $image_size = $_FILES[$field]['size'];
    $image_ext = pathinfo($image_name, PATHINFO_EXTENSION);
    $image_path = '../clientes/img/' . $image_name;
    if ($image_size < 10000000) {
        if ($image_ext == 'jpg' || $image_ext == 'png' || $image_ext == 'jpeg' || $image_ext == 'gif') {
            return move_uploaded_file($image_tmp, $image_path);
        }
    }
    return false;
}



if (isset($_POST['submit_post'])) {
    $title = strip_tags($_POST['title']);
    $date = date('Y-m-d h:i:s');
    if ($_FILES['image']['name'] != '') {

        $upload = imageUpload('image');
        $image_db_path = 'img/' . $_FILES['image']['name'];
        $upload1 = imageUpload('img_v1');
        $image_db_path1 = 'img/' . $_FILES['img_v1']['name'];
        $upload2 = imageUpload('img_v2');
        $image_db_path2 = 'img/' . $_FILES['img_v2']['name'];

        if ($upload && $upload1 && $upload2)
        $ins_sql = "INSERT INTO gallery (title, description, image, img_v1,  img_v2, category, status) VALUES ('$title', '$_POST[description]',
'$image_db_path', '$image_db_path1', '$image_db_path2', '$_POST[category]',   '$_POST[status]')";

    }
}

Upvotes: 1

Related Questions