double820122 2016-06-27 03:21
浏览 75
已采纳

如何上传图片并与其当前帐户会话的“id”一起保存到数据库中

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 
";
                echo "
 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:
enter image description here

Any help would be highly appreciated.

  • 写回答

1条回答 默认 最新

  • dougudu3564 2016-06-27 05:51
    关注

    php code:

        $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']."')
        ");  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。