lundi 27 juin 2016

How to upload picture and save into database together with the 'id' of its current session of an account

The code below can save the filename of the image together with the file extension into database. My problem is i don't know how to get the id and save it simultaneously with picture. Here is my code so far:
Session PHP

$id = $_SESSION['id']; 

Modal for Upload picture:

                    <!-- Modal content-->
                    <div class="modal-content">
                      <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">Profile Picture</h4>
                      </div>
                      <div class="modal-body">
                        <form id="upload_image" enctype="multipart/form-data" action="" method="post">
                                    <div id="filediv">
                                        <label>Chose image:</label><input  style="margin: 0 auto;" name="file3[]" type="file" id="file3"/>
                                    </div>
                                <input  type="text" name="prop_number" value="<?php echo "1";  ?>" style="display:none" />
                                </form>
                      </div>
                      <div class="modal-footer">
                          <button type="button" class="btn btn-success" data-dismiss="modal" id="upload22"><i class="fa fa-upload"></i> Upload</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal"><i class="fa fa-close"></i> Close</button>
                      </div>
                    </div>


                  </div>
                </div>

JQuery Code:

 //Upload Picture
 $('#upload22').click(function(e) { 
    var selper = $('#selectperson').val();
    var trythis = $('#file3').val();
    //if(selper == '' || selper == 'Select Person'){
    //sweetAlert({title: 'Please Select Person',   text: 'Please choose a file',   type: 'warning',      closeOnConfirm: true,   showLoaderOnConfirm: true, });     
    //}
    if(trythis == ''){
    sweetAlert({title: 'Missing File.. File is required',   text: 'Please choose a file',   type: 'warning',      closeOnConfirm: true,   showLoaderOnConfirm: true, });    
    }
    else{
    var data1 = new FormData();
        jQuery.each(jQuery('#file3')[0].files, function(i, file) {
            data1.append('file3[]', file);
        });     
    var other_data = $('#upload_image').serializeArray();
        $.each(other_data,function(key,input){
            data1.append(input.name,input.value);
    });     

        jQuery.ajax({
            url: 'queries_james.php',
            data: data1,
            cache: false,
            contentType: false,
            processData: false,
            type: 'POST',           

            success: function(response){
                sweetAlert({title: 'Successfully uploaded.',   text: 'Thank you',   type: 'success',      closeOnConfirm: false,   showLoaderOnConfirm: true, }, function(){   window.location.href = 'add_gen.php'; });    
                updater();
            }
        });  
    }
});

Query to database:

if (isset($_FILES["file3"])) { 

//$target_path = "uploads/"; //Declaring Path for uploaded images
$id = "6";//$_POST['id'];
for ($i = 0; $i < count($_FILES['file3']['name']); $i++) {//loop to get individual element from the array

    $validextensions = array("png", "jpg", "JPG", "PNG","jpeg","JPEG");  //Extensions which are allowed
    $ext = explode('.', basename($_FILES['file3']['name'][$i]));//explode file name from dot(.) 
    $file_extension = end($ext); //store extensions in the variable
    $j = 0; //Variable for indexing uploaded image 
    $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];//set the target path with a new name of image
    $j = $j + 1;//increment the number of uploaded images according to the files in array       

  //if (($_FILES["file3"]["size"][$i] < 1073741824) //Approx. 100kb files can be uploaded. original
  if (($_FILES["file3"]["size"][$i] < 10000001073741824)
            && in_array($file_extension, $validextensions)) {
        if (move_uploaded_file($_FILES['file3']['tmp_name'][$i],
        "profile/".basename($_FILES['file3']['name'][$i]))) {
            //if file moved to uploads folder
        if(isset($_FILES["file3"])){
            $link1=basename($_FILES['file3']['name'][$i]);  
            //$id=$_POST["prop_number"];    
            $findings= pg_query($connection, "
            insert into tbl_profile_picture(id,name)
            values ((SELECT COALESCE( max(id),0)+1 FROM tbl_profile_picture),'$link1')
            ");  

            if($findings){
                //echo "List Updated n";
                echo "n Save successfully!.";
            }
            else{
                //echo $link;
                echo "error";
            }
        }
        }


        else {//if file was not moved.
        echo $link;
            echo $j. ').<span id="error">please try again!.</span><br/><br/>';
        }
    } else {//if file size and file type was incorrect.
        echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
    }
}

}

Database Structure:
Screenshot of my database

Any help would be highly appreciated.

Aucun commentaire:

Enregistrer un commentaire