Click here to Skip to main content
15,921,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created multiple file form. But when i insert files in database it inserted separated file each row.So i want to insert multiple file in one row.

What I have tried:

$media_tittle=$_POST['media_tittle'];	
	
	$extension = array("jpeg","jpg","png");	
	// Count total files
	$countfiles = count($_FILES['media_images']['name']);
	
	for($i=0;$i<$countfiles;$i++){
		$filename = $_FILES['media_images']['name'][$i];
		$tmp_dir = $_FILES['media_images']['tmp_name'][$i];
		$imgSize = $_FILES['media_images']['size'][$i];		
	
		$namefile = pathinfo($filename, PATHINFO_FILENAME);
		$ext = pathinfo($filename, PATHINFO_EXTENSION);		
		
		$counter = 0;
		
		if(empty($media_tittle)){
			$errMSG = true;	
			echo "Please Enter Title.";
		}
		elseif(empty($filename)){
			$errMSG = true;	
			echo "Please Upload Images.";
		}
		elseif(in_array($ext, $extension) == false){
			$errMSG = true;	
			echo "Please Upload .jpeg, .jpg, .png formatted file.";
		}
		elseif($imgSize >= 500000){
			$errMSG = true;	
			echo "File too large. File must be less than 500 Kilobytes.";
		}
		if(!isset($errMSG)){
			list($b_width, $b_height) = getimagesize($tmp_dir);
			if($b_width != 730 || $b_height != 500){
				$errMSG = true;	
				echo "Please check the Witdh and height of Image.";
			}
			if(!isset($errMSG)){				
				while(file_exists("../media_corner/$filename")){
					$filename = $namefile. $counter.'.'.$ext;
					$counter++;
				}
				move_uploaded_file($tmp_dir, "../media_corner/$filename");				
				//$filename = $_FILES['media_images']['name'];
				$result = mysqli_query($conn, "INSERT INTO `media_corner`(`title`, `images`, `status`) VALUES ('$media_tittle','$filename','N')")or die("Could not retrieve image: " . mysqli_error($conn));
			}		
		}		
	}
Posted
Updated 21-Nov-18 2:44am

1 solution

Two things:

1) You have designed your table for one file/row - so that's what it holds.
2) You could make multiple sets of similar valued columns, one for each file you wish to hold, and add them. And empty ones are a waste.
3) You can keep doing it with the table you have - which is the right way.

If you want to add more titles/row, you need to have a set of columns for each file and cannot have any more than what you have designated as columns. On the other hand, if you have one per row, all you need do is keep track of what group they're in and can have as many per insertion group as you wish.

Why not add a column to associate them - a random number, date-time stamp, or other unique item so that you can group them when inserted. I would add an identity field, as well, and use the identity value from the first of a group to fill the associative column for the entire group.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900