Uploading multiple image file to a database at once

hi guys i have a challenge of inserting multiple image at once.
one image goes into the db from the array of images i selected .
here is my code thanks.

if (isset($_POST[‘submit’]))


{			//list out your form variables...
		$property_name=$_POST['property_name'];
		$property_location=$_POST['property_location'];
		//$property=$_FILES['property']['name'];
		$property_price=$_POST['property_price'];
		$category_id=$_POST['category_id'];
		$status=$_POST['status'];

		if (empty($property_name)){
					echo "Enter Property Location.";
				}elseif (empty($property_location)) {
					echo "Description";
					# code...
				}elseif (empty($property_price)) {
					echo "Enter Price.";
					# code...
				}
				elseif(empty($category_id)){
					echo "category_id Feild is required";
				}elseif (empty($status)){
					echo "Indicate Status of Property";
					# code...
				}else{
					//validation

       $sql="INSERT INTO `property` (property_name, property_location, property_price, category_id, status)VALUES (:property_name, :property_location, :property_price, :category_id, :status)";

      	     $params=[':property_name'=>$property_name, 
      	              ':property_location'=>$property_location,
      	              ':property_price'=>$property_price, 
					  ':category_id'=>$category_id, 
					  ':status'=>$status];

	    //var_dump($db->runQuery($sql,$params));
						$inserting=$db->runQuery($sql,$params);

						if ($inserting==1)
						{			
									
							$pro_id= $db->lastId();
							var_dump($pro_id);
                              $property_img=$_FILES['property_img']['name'];
	                          if (empty($property_img))
	               
	                          	{ echo "Enter Property Images.";}

	                          // uploading image file.
					
				   else{
				   	     $image_name='';
				$images_name=array();
				foreach($_FILES['property_img']['tmp_name'] as $i => $tmp_name ){

					 $property_img = $_FILES['property_img']['name'][$i];
                    $file_size = $_FILES['property_img']['size'][$i];
                    $file_tmp = $_FILES['property_img']['tmp_name'][$i];
				 				    
				 				    \
		        /* Less complicated and more reliable method to find the file extension!! */
                    $file_ext = strtolower( pathinfo( $property_img, PATHINFO_EXTENSION ) );
                     $ext_boleh = array( 'jpg', 'jpeg', 'png', 'gif', 'bmp' );
                      if( in_array( $file_ext, $ext_boleh ) ) {
			         $uploads_dir = 'uploads/';
			         $add= move_uploaded_file($file_tmp, $uploads_dir. $property_img);
			             print_r($property_img);


$sql2=“INSERT INTO  `property_img`  (property_img, pro_id)VALUES (?,?)”;


					$params=[$property_img,$pro_id];

      	              $img=$db->runQuery($sql2,$params);

                  if($img!=0){
                  	echo "file uploaded successfully";

                  }
                  else {
                  	echo "Error in file Upload.";
                  }

Prepare the image insert query before the foreach, then execute it inside the foreach so it does it for all images.

i did these and i have a database class for all my query.

if ($inserting==1)
					{			
						$pro_id= $db->lastId();
						var_dump($pro_id);
                                                $property_img=$_FILES['property_img']['name'];

and here is my database class.

public function runQuery($query,$params=[]){
			try{		
			$stmt=$this->datab->prepare($query);
			$x=$stmt->execute($params);
			return $x;
			var_dump($x);
			
			}catch (PDOException $e){
			throw new Exception($e->getMessage());
			}
		}

Yes you could call that method within the foreach and it would work. Those methods are handy for one-off prepared statements.
It’s just it would be more efficient to prepare once and execute multiple times.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.