How to insert multiple images path to database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ghjk
    Contributor
    • Jan 2008
    • 250

    How to insert multiple images path to database

    In my application I want to store vehicle details to mysql database. There is image field which is having more than 1 images.(select from the user) It may be 3 ,4 ,5 etc.image table has image id and image path field.When submit button click I want to add images to vehicle folder(this part working) and all images paths to the database. Really I'm wired now. I don't know how to add multiple images record to the database. Please help me. This is my existing code.

    Code:
    if (isset($_POST['Submit'])) {
    
    	$vehicles_pk = mysql_insert_id(); // get last inserted vehicle id
        $number_of_file_fields = 0;
        $number_of_uploaded_files = 0;
        $number_of_moved_files = 0;
        $uploaded_files = array();
        $upload_directory = dirname(__file__) . '/vehicles/'; //set upload directory
       
        // * we get a $_FILES['images'] array ,
        // * we procee this array while iterating with simple for loop 
        // * you can check this array by print_r($_FILES['images']); 
         
        for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
            $number_of_file_fields++;
            if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not
                $number_of_uploaded_files++;
                $uploaded_files[] = $_FILES['images']['name'][$i];
                if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) {
                    $number_of_moved_files++;
    			
                }
    
            }
     
        }
    $name= implode(',', $uploaded_files);
    echo $name; // this will print 1.jpg,2.jpg
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    HOw about this structture?
    AutoId-----UserId-----UserPicture
    1-----User1-----UserPicture1
    2-----User2-----UserPicture2
    3-----User3-----UserPicture3

    Comment

    • ghjk
      Contributor
      • Jan 2008
      • 250

      #3
      Actually I want the help of insert query. I don't know how to do that for multiple values.(one field has multiple values)

      Comment

      • ghjk
        Contributor
        • Jan 2008
        • 250

        #4
        I have done it. This is the code..
        Code:
        if (isset($_POST['Submit'])) {
        	echo $_SESSION['vehicles_pk'];
            $number_of_file_fields = 0;
            $number_of_uploaded_files = 0;
            $number_of_moved_files = 0;
            $uploaded_files = array();
            $upload_directory = dirname(__file__) . '/vehicles/'; //set upload directory
            // * we get a $_FILES['images'] array ,
            // * we procee this array while iterating with simple for loop 
            // * you can check this array by print_r($_FILES['images']); 
             
            for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
                //$number_of_file_fields++;
                if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not
                    $number_of_uploaded_files++;
                    $uploaded_files[] = $_FILES['images']['name'][$i];
                    if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) {
                       $number_of_moved_files++;
        				//echo $number_of_moved_files++;
        				$sql ="INSERT INTO images  VALUES ('', '".$_SESSION['vehicles_pk']."','".$upload_directory . $_FILES['images']['name'][$i]."')";
        				
        				$result = mysql_query($sql);
        	
                    }
        
                }
        		unset($_SESSION['vehicles_pk']);
         
            }
        
        }

        Comment

        Working...