javascript - 如何上传图片并与帐户当前 session 的 'id'一起保存到数据库中

标签 javascript php jquery html postgresql

下面的代码可以将图像的文件名和文件扩展名一起保存到数据库中。我的问题是我不知道如何获取 id 并将其与图片同时保存。这是到目前为止我的代码:
session PHP

$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

任何帮助将不胜感激。

最佳答案

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']."')
    ");  

关于javascript - 如何上传图片并与帐户当前 session 的 'id'一起保存到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38045420/

相关文章:

javascript - Mongoose 将请求对象传递给中间件

javascript - 如何在同一个页面中制作多个阅读更多阅读更少按钮

javascript - 我如何在 java 脚本中使用回车键触发按钮?/如何给文本框和搜索按钮一个 ID?

jquery - 让一段文字位于正上方

javascript - 是否可以将文件附加到 JavaScript 中的电子邮件?

javascript:为什么这个 return 语句会导致语法错误?

javascript - 解析对象的历史实现

php - 如何用mysql制作一个未读主题列表

php - CodeIgniter3 : Why would $_SERVER ['CI_ENV' ] ever be set in the first place?

php - Wordpress 上传视频转换 MP4 格式