0

enter image description here

if(count($_FILES['upload']['name']) > 0){ 

    //Loop through each file 
    for($i=0; $i<count($_FILES['upload']['name']); $i++) { 

        //Get the temp file path 
        $tmpFilePath = $_FILES['upload']['tmp_name'][$i]; 

        //Make sure we have a filepath 
        if($tmpFilePath != ""){ 

            //save the filename 
            $shortname = $_FILES['upload']['name'][$i]; 

            //save the url and the file 
            $filePath = "../img/slider/" .$_FILES['upload']['name'][$i]; 

            //Upload the file into the temp dir             
            if(move_uploaded_file($tmpFilePath, $filePath)) { $files[] = $shortname; 


            } 
        } 
    } 
}

if(is_array($files)){ 

    foreach($files as $file){


        $file1 = "img/slider/" . $file; 
        $query = "INSERT INTO image(image_path,p_number)VALUES('$file1','$number')";
        $result3 = $db->insert($query);
    } 
}

How can I add implode? to store in my database as a 1 record only. because this is generating record each of file my file upload.

enter image description here

the first image is my current database look like. and the second is my goal. how can I do that?

George Kagan
  • 5,913
  • 8
  • 46
  • 50
oo7
  • 59
  • 1
  • 6
  • I'm not sure I understand the question? Should all file data be stored in one single row? Edit your question and add an example of how you would like the row to look in your database. And what is `$number`? – Cyclonecode Nov 10 '16 at 08:28
  • 2
    _How_ should those paths and numbers be imploded so that they can still be used afterwards? Certainly keeping those entries separate is a very good thing. That is how relation databases are meant to be used. – arkascha Nov 10 '16 at 08:29
  • 1
    And by the way: your code is vulnerable to sql injection attacks. You do _not_ want to use the client side specified file name for your local storage. First because of said injection attacks and Second because the same file name might get used multiple times, you'd have a collision. You want to compute an internal file name based on the current timestamp and maybe a user id. – arkascha Nov 10 '16 at 08:30
  • http://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad/3653574 – e4c5 Nov 10 '16 at 09:00

3 Answers3

0

Beside it should for the most scenarios not the best way to store multiple values into a single row-column, you can update the column if the p_number (used as primary key) are already exists on insert:

INSERT INTO image(image_path,p_number)VALUES('$file1','$number')
ON DUPLICATE KEY UPDATE image_path = CONCAT(image_path, ',', $file1);
take
  • 2,202
  • 2
  • 19
  • 36
0

According to your requirement, you can change your code like this:

if(is_array($files)){ 
       $completeFileName = implode(',',$files);       
       $query = "INSERT INTO image(image_path,p_number)VALUES('$completeFileName','$number')";
        $result3 = $db->insert($query);
}
Anshul
  • 116
  • 1
  • 10
0

Use PHP Implode

if(is_array($files)){ 
$query = "INSERT INTO image(image_path,p_number)VALUES('".implode(",", $files)."','$number')";
}

Sidenote: You are storing img/slider for all images. You can remove that and store as Penguins,Jellyfish alone in your DB against the number and where you display the image you can append it to the src. You can reduce the size of the table.

Sarvap Praharanayuthan
  • 4,212
  • 7
  • 47
  • 72