1

Mates Please Help me to solve this problem. I ve created one from where multiple Image can Insert to a single row of database now my code is working well but it is not possible to store all image in a single data base row it is storing in multiple rows. Here what i ve done till yet.

<?php
mysql_connect("localhost","root","");
mysql_select_db("test");



$uploads_dir = 'photo/';
foreach ($_FILES["image"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["image"]["tmp_name"][$key];
        $name = $_FILES["image"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
        $sql=mysql_query("INSERT INTO multiimg SET image='$name'");
    }
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function addmore(num)
{
    if(num==1)
    {
        document.getElementById('field2').style.display='block';
        document.getElementById('ni1').style.display='block';
        return false;
    }
    else if(num==2)
    {
        document.getElementById('field3').style.display='block';
        return false;
    }

}
</script>
</head>

<body>
<form enctype="multipart/form-data" name="" action="" method="post">
    <div id="field1">Enter One Image :<input type="file" name="image[]" id="img1"/><a href="#" onclick="addmore(1)" id="ni1">addmore...</a></div>
    <div id="field2"  style="display:none;">Enter Two Image :<input type="file" name="image[]" id="img2"/><a href="#" onclick="addmore(2);">add more...</a></div>
    <div id="field3"  style="display:none;">Enter Three Image :<input type="file" name="image[]" id="img3"/><a href="#" onclick="addmore(3)" id="ni3">addmore...</a></div>
    <div id="field4" style="display:none">Enter Forth Image :<input type="file" name="image[]" id="img4"/><a href="#" onclick="addmore(4)" id="ni4">addmore...</a></div>

    <input type="submit" name="submit"/>

</form>

</body>
</html>

enter image description here

11
  • 2
    You can insert it comma separated. Commented Dec 17, 2014 at 7:08
  • Do you want 'INSERT' or 'UPDATE' query? If 'INSERT' then your query is wrong. Commented Dec 17, 2014 at 7:09
  • with separator also not possible @ツFellinLovewithAndroidツ Commented Dec 17, 2014 at 7:10
  • I like to INSERT operation Commented Dec 17, 2014 at 7:11
  • INSERT operation will create separate row for each image in database, and your problem statement says you want to insert in single row. Commented Dec 17, 2014 at 7:13

6 Answers 6

3

Try below code

$images_name ="";
    foreach ($_FILES["image"]["error"] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {
            $tmp_name = $_FILES["image"]["tmp_name"][$key];
            $name = $_FILES["image"]["name"][$key];
            move_uploaded_file($tmp_name, "$uploads_dir/$name");
            $images_name =$images_name.",".$name;
        }
    }

    $sql=mysql_query("INSERT INTO multiimg(image) values('".$images_name."')");
Sign up to request clarification or add additional context in comments.

5 Comments

It is working but before each time inserting to database it is taking one blank row have you checked @Miya G
If its working for your then, you can modify this code according to your comfort.
@sameerkumar why are you using set condition in your sql query? every time previous values will replace with new one. Have you noticed that?
I cant able to avoid the pre inserting row before the name are inserting @Miya G any help ll be appreciate.
3

You can use serialize() function for storing the multiple image name in single row. Use the below code:

$uploads_dir = 'photo/';

$imageArr = array();

foreach ($_FILES["image"]["error"] as $key => $error) {

if ($error == UPLOAD_ERR_OK) {
    $tmp_name = $_FILES["image"]["tmp_name"][$key];
    $name = $_FILES["image"]["name"][$key];
    move_uploaded_file($tmp_name, "$uploads_dir/$name");
    array_push($imageArr,$name);
}

}

$imageArr=serialize($imageArr);

$sql=mysql_query("INSERT INTO multiimg SET image='".$imageArr."'");

After that use unserialize() function,this will show the array of that multiple image. user the bellow code:

$sql1=mysql_query("Select * from multiimg");

$result=mysql_fetch_assoc($sql1);

print_r(unserialize($result['image'])); 

1 Comment

Just correct the insert query and worked like a charm for me.
0

hint not exact code

your loop and run loop for multiple image name and store them in single variable

   foreach( $_FILES["image"]["name"] as $key=>$value){
    $name .=$value;
  }

  $sql=mysql_query("INSERT INTO multiimg (`fieladname`) VALUES (insertField));

this will store same name to all row i hope this will solve your problem

2 Comments

What 'SET' is supposed to do in 'INSERT' query?
sorry i just copy users answer .... SET keyword use with update so change as per your operation
0

You can do this in two ways, either by concatenating image names as answered by @Miya and @Affan or by creating separate column for each image.

I'll explain second approach.

You have four fixed image upload buttons. So instead of having single column 'image' in db table you can have four columns each for separate image.

So you table schema will be

id   |   image1   |   image2   |   image3   |   image4 

And now you can insert all images using single insert query as,

$uploads_dir = 'photo/';
$name = array();
$i=1;
foreach ($_FILES["image"]["error"] as $key => $error) {
    $name[$i] == "";
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["image"]["tmp_name"][$key];
        $name[$i] = $_FILES["image"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
        $sql=mysql_query("INSERT INTO multiimg SET image='$name'");
    }
    $i++;
}
$sql=mysql_query("INSERT INTO multiimg (`image1`,`image2`,`image3`,`image4`) VALUES ('$name[1]','$name[2]','$name[3]','$name[4]')");

I recommend you this approach because in future if you want to fetch image name then it will easier than the value inserted by first approach.

1 Comment

The image field is not limited @rack_nilesh
0

Try something like this:

$uploads_dir = 'photo/';
$imageArr = array();
foreach ($_FILES["image"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["image"]["tmp_name"][$key];
        $name = $_FILES["image"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
        array_push($imageArr,$name);
    }
}
$sql=mysql_query("INSERT INTO multiimg SET image='".json_encode($imageArr)."'");

When you selected it from the database, you can use json_decode to get your images in an array.

3 Comments

This bunch is taking null value into database and only the last image name is going to the table field and also it is taking separate rows @FIA2008
Because of INSERT INTO every time you run the script images is inserted in new row. Change query to UPDATE, but you need to have at least one row first. Just update image cell with new data. First, select that row, then append new values with the old one, and update.
Sorry, forgot to change $name into imageArr when using json_encode
0
       $img[$i] = array();            
       $uploads_dir = "images/";
       $i=1;
       foreach ($_FILES["image"]["error"] as $key => $error)
        {
         $name[$i] == "";
         if  ($error == UPLOAD_ERR_OK) {
              $tmp_name = $_FILES["image"]["tmp_name"][$key];
              $img[$i] = $_FILES["image"]["name"][$key];
              move_uploaded_file($tmp_name, "$uploads_dir/$img[$i]");
                                       }
              $i++;
        }
    $sql1 = "INSERT INTO event(img1,img2,img3) VALUES('$img1','$img2','$img3')";

1 Comment

I wonder if this is vulnerable to SQL injection attacks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.