0

I'll rephrase my question.

My problem is not storing only images.. but storing multiple images alongside with text, description, etc.. I researched all over stack overflow finding for the answer but most of what I found was answers related to storing images only. I would like to find how can I insert multiple data(name, image, description) at the same time.

The only part I am having difficulty is the multiple image part.

Also, my form is dynamically generated using jquery.

my db structure would be like

id | name | image | description | price

This is my HTML:

<td><input type="text" class="form-control" name="item[]" placeholder="Name of Item" /></td>
<td><input type="text" class="form-control" name="desc[]" placeholder="Description" /></td>
<td><input type="text" class="form-control" name="price[]" placeholder="Price" /></td>
<td><input type="text" class="form-control" name="brand[]" placeholder="Brand" /></td>
<td><input type="file" name="images" class="form-control" /></td>
<input type="submit" id="save-product" class="btn btn-success" name="submit" value="Submit">

This is my PHP:

if(isset($_POST['submit']) ){
$name = $_FILES['images']['name'];
$type = $_FILES['images']['type'];
$size = $_FILES['images']['size'];
$temp = $_FILES['images']['tmp_name'];
$error = $_FILES['images']['error'];
$allowed = array(
    "image/jpeg",
    "image/jpg",
    "image/png");
$con = mysqli_connect("localhost","root", "", "db_elective");
if(in_array($type, $allowed) && $size < 2000000 && !empty($_POST['item'])){
    $ext = end(explode(".", $name));
    $newFilename = sha1(date("Y-m-d h:i:s")) . "." . $ext;
    if(move_uploaded_file($temp, "../uploaded" . $newFilename)){
        
        $item = $_POST['item'];
        $desc = $_POST['desc'];
        $price = $_POST['price'];
        $brand = $_POST['brand'];
        $image = $newFilename;

        for($x = 0; $x < count($item); $x++){
            $vItem = $item[$x]; 
            $vdesc = $desc[$x];
            $vbrand = $brand[$x];
            $vImg = $image[$x];
            $vprice = $price[$x];
            mysqli_query($con, "INSERT INTO tb_items (product_name, product_desc, product_img, product_brand, price) 
                VALUES('$vItem','$vdesc', '$vImg', '$vbrand', '$vprice')");
        }
    

    }

}else{
    echo "Wrong";
}

}
Community
  • 1
  • 1
eaponz
  • 574
  • 1
  • 16
  • 32
  • 1
    You should really switch to prepared statements. – jeroen Dec 21 '13 at 16:12
  • possible duplicate of [PHP multiple image file upload and storage to folder and database](http://stackoverflow.com/questions/4923866/php-multiple-image-file-upload-and-storage-to-folder-and-database) – Glavić Dec 21 '13 at 16:12

2 Answers2

0

firstly change your form if you are taking multiple images at same time, --

<td><input type="file" name="images[]" class="form-control" /></td>
<td><input type="file" name="images[]" class="form-control" /></td>

and then you can take multiple images at same time -- and then process the images using foreach loop --i.e. to say just by uploading one by one and inserting a new record for each one.

Abhishek
  • 1,543
  • 3
  • 13
  • 29
  • how can I insert it? along with the other values(item name, description etc..) .. for loop or foreach? – eaponz Dec 21 '13 at 18:04
0

You can use PHP's array notation as you would in regular form fields:

Image 1:  <input type="file" name="images[]" class="form-control" />
Image 2:  <input type="file" name="images[]" class="form-control" />
Kiran RS
  • 981
  • 14
  • 31
  • how can I insert it? along with the other values(item name, description etc..) .. for loop or foreach? – eaponz Dec 21 '13 at 16:34