2016-06-27 7 views
0

以下のコードは、ファイルの拡張子とともに画像のファイル名をデータベースに保存することができます。私の問題は、IDを取得して画像と同時に保存する方法がわかりません。ここに私のコードは、これまでのところです:
セッションPHP画像をアップロードして、アカウントの現在のセッションの「ID」と一緒にデータベースに保存する方法

$id = $_SESSION['id']; 

アップロード画像のためのモーダル:

    <!-- 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コード:データベースへ

//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(); 
      } 
     }); 
    } 
}); 

問合せ:

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/>'; 
    } 
} 

}

データベース構造:
enter image description here

すべてのヘルプは高く評価されるだろう。

+0

あなただけの '$ _SESSION [ 'ID']を使用することはできません;'データベースクエリ内の? – andrewsi

+0

Sir @andrewsi?どのように私のクエリでそれを実装するには?私にいくつかのコードを教えてもらえますか? –

+0

'$ _SESSION ['id']'を名前とIDと共に挿入したいですか? (SELECT COALESCE(max(id)、0)+1 'の代わりに –

答えて

0

PHPコード:

$findings= pg_query($connection, " 
    insert into tbl_profile_picture(id,name,emp_id) 
    values ((SELECT COALESCE(max(id),0)+1 FROM tbl_profile_picture),'$link1','".$_SESSION['id']."') 
    "); 
関連する問題