www.webdeveloper.com
Results 1 to 9 of 9

Thread: Get names from foreach and insert them in the db

Hybrid View

  1. #1
    Join Date
    Aug 2010
    Posts
    20

    Get names from foreach and insert them in the db

    Hello,
    I have a simple image multi uploader that uploads images on the folder correctly but i don't know how to get the image names and insert them in the db. The uploader is part of a complex form for adding an article informations (id, name, permanenturl, section, etc) that works correctly.
    Image fields in db has names as follows: img1 img2 img3 img4 im5, a field for each image (since i thought that this is the best way to add the images on the article).
    The upload sections looks like this:
    PHP Code:
    <?php



    /**

    * Smart Image Uploader by @cafewebmaster.com

    * Free for private use

    * Please support us with donations or backlink

    */





    $upload_image_limit 5// How many images you want to upload at once?

    $upload_dir    "/var/www/tour/te-ngarkuara/oferta/"// default script location, use relative or absolute path

    ################################# UPLOAD IMAGES

            
    foreach($_FILES as $k => $v){ 

                
    $img_type "";

                
    ### $htmo .= "$k => $v<hr />";     ### print_r($_FILES);

                
    if( !$_FILES[$k]['error'] && preg_match("#^image/#i"$_FILES[$k]['type']) && $_FILES[$k]['size'] < 1000000){

                    
    $img_type = ($_FILES[$k]['type'] == "image/jpeg") ? ".jpg" $img_type ;

                    
    $img_type = ($_FILES[$k]['type'] == "image/gif") ? ".gif" $img_type ;

                    
    $img_type = ($_FILES[$k]['type'] == "image/png") ? ".png" $img_type ;

                    
    $data = (date("jnHi")); // Merr daten dhe oren ne momentin e shtimit te fotografise

                    
    $img_rname $data."-".$_FILES[$k]['name']; // shton daten dhe oren perpara emrit te fotografise per te krijuar emer unik
                    
    $img_rname str_replace(" ","-",$img_rname); // Heq hapesirat dhe i zevendeso me -

                    
    $img_path $upload_dir.$img_rname;

                    
    copy$_FILES[$k]['tmp_name'], $img_path );
                    
    chmod("$img_path",0777); 

                    
    $feedback .= "Image and thumbnail created $img_rname<br />";

                }

            }



    ############################### HTML FORM

        
    while($i++ < $upload_image_limit){

            
    $form_img .= '<label>Image '.$i.': </label> <input type="file" name="uplimg'.$i.'"><br />';

        }



        
    $htmo .= '

            <p>'
    .$feedback.'</p>

            <form method="post" enctype="multipart/form-data">

                '
    .$form_img.' <br />

                <input type="submit" value="Upload Images!" style="margin-left: 50px;" />

            </form>

            '
    ;    



        echo 
    $htmo;
    Can anyone help me on this, please?

    Thank you in advance!

  2. #2
    Join Date
    Jan 2011
    Posts
    62
    I added lines 24 and 45, this will give you an array of the image names that you can them step through to create an insert statement for you bd

    PHP Code:
    <?php



    /**

    * Smart Image Uploader by @cafewebmaster.com

    * Free for private use

    * Please support us with donations or backlink

    */





    $upload_image_limit 5// How many images you want to upload at once?

    $upload_dir    "/var/www/tour/te-ngarkuara/oferta/"// default script location, use relative or absolute path

    ################################# UPLOAD IMAGES
            
    $imageNames = Array(); //nova
            
            
    foreach($_FILES as $k => $v){

                
    $img_type "";

                
    ### $htmo .= "$k => $v<hr />";     ### print_r($_FILES);

                
    if( !$_FILES[$k]['error'] && preg_match("#^image/#i"$_FILES[$k]['type']) && $_FILES[$k]['size'] < 1000000){

                    
    $img_type = ($_FILES[$k]['type'] == "image/jpeg") ? ".jpg" $img_type ;

                    
    $img_type = ($_FILES[$k]['type'] == "image/gif") ? ".gif" $img_type ;

                    
    $img_type = ($_FILES[$k]['type'] == "image/png") ? ".png" $img_type ;

                    
    $data = (date("jnHi")); // Merr daten dhe oren ne momentin e shtimit te fotografise

                    
    $img_rname $data."-".$_FILES[$k]['name']; // shton daten dhe oren perpara emrit te fotografise per te krijuar emer unik
                    
    $img_rname str_replace(" ","-",$img_rname); // Heq hapesirat dhe i zevendeso me -
                    
                    
    $imageNames[] = img_rname// Nova
                    
                    
    $img_path $upload_dir.$img_rname;

                    
    copy$_FILES[$k]['tmp_name'], $img_path );
                    
                    
    chmod("$img_path",0777);

                    
    $feedback .= "Image and thumbnail acreated $img_rname<br />";

                }

            }



    ############################### HTML FORM

        
    while($i++ < $upload_image_limit){

            
    $form_img .= '<label>Image '.$i.': </label> <input type="file" name="uplimg'.$i.'"><br />';

        }



        
    $htmo .= '

            <p>'
    .$feedback.'</p>

            <form method="post" enctype="multipart/form-data">

                '
    .$form_img.' <br />

                <input type="submit" value="Upload Images!" style="margin-left: 50px;" />

            </form>

            '
    ;    



        echo 
    $htmo;

  3. #3
    Join Date
    Aug 2010
    Posts
    20
    thank you for the reply fideltfg, but, since i'm new on php/mysql, can you explain more the way you suggest?
    Thank you very much!

  4. #4
    Join Date
    Jan 2011
    Posts
    62
    ok so after your foreach statement $imageNames will contain an array of all the image file names

    you can then create a query like this with it
    PHP Code:
    $c count $imageNames );
    $query "INSERT INTO YOURTABLENAME (img1, img2, img3, img4, im5) VALUES (";

    foreach ( 
    $imageNames as $name ) {
        
    $query .= $name;
        if (
    $c 1) {
            
    $query .=", ";
            
    $c--;
        }
    }
    $query .= ")"
    then send that query to your database how ever your doing that.

  5. #5
    Join Date
    Aug 2010
    Posts
    20
    Thank you. I have added the code you provided with minor changes in variable names, but i'm getting an error on submit:
    Code:
    Fatal error: Column count doesn't match value count at row 1
    Thanks!

  6. #6
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by Kleidi View Post
    Thank you. I have added the code you provided with minor changes in variable names, but i'm getting an error on submit:
    Code:
    Fatal error: Column count doesn't match value count at row 1
    Thanks!
    You are missing the columns list. See the reply above showing the expected syntax.

  7. #7
    Join Date
    Dec 2010
    Location
    Romania
    Posts
    27
    put at the end of the script a
    PHP Code:
    echo($query); 
    and then load the page again .Execute the query in phpmyadmin and you'll find the error...

  8. #8
    Join Date
    Mar 2009
    Location
    Texas
    Posts
    124
    why not put them all in the same field? seperated by a coma or create a new row for each one in the database?

  9. #9
    Join Date
    Mar 2009
    Location
    Texas
    Posts
    124
    Something like this would create a new row for each image
    PHP Code:
    $numoffile 5;
          
    // Fix path of your file to be uploaded, don't forget to CHMOD 777 to this folder
          
    $file_dir "/home/link/images/house/".$id."/"//generate directory
          
    mkdir($file_dir0777);
        if (isset(
    $_POST['upload'])) {
                if(
    count($_FILES['myfiles']['name'])>0) { //check if any file uploaded
                  
    for($i=0$i count($_FILES['myfiles']['name']); $i++) {//loop the uploaded file array
                      
    if (trim($_FILES['myfiles']['name'][$i])!="") { 
                              
    $newfile $file_dir.$_FILES['myfiles']['name'][$i]; //file name
                              
    move_uploaded_file($_FILES['myfiles']['tmp_name'][$i], $newfile);//upload the file
                              
    $j++;
                              
    $my_array[] = $_FILES['myfiles']['name'][$i]; 
                              
    $pics array_merge($my_array);
                      }    
                     
                  }

                  
                }
        }
                
            foreach (
    $pics as $t => $v ) {
                
    $sql_pic "INSERT INTO `images` (`image`, `cat_id`, `list_id`) VALUES ('".$v."', '1', '".$id."')  ";
                
    $result_pic = @mysql_query($sql_pic);
                    if (
    $result_pic){
                        echo 
    '<tr>
                    <td colspan="2" class="form_text3">File:'
    .$v.' has been added to the database<br />';
                    }
        } 
    to put them all in the same field you could implode pics with
    PHP Code:

    implode
    (', '$pics); 
    Last edited by ebar; 09-22-2011 at 12:55 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles