下面的代码可以将图像的文件名和文件扩展名一起保存到数据库中。我的问题是我不知道如何获取 id 并将其与图片同时保存。这是到目前为止我的代码:
session PHP
$id = $_SESSION['id'];
上传图片模式:
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</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/>';
}
}
}
任何帮助将不胜感激。
最佳答案
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/