jQuery通过PHP上传图像并将其插入MySQLi BLOB
我在将图像上传到MySQL并将其保存为BLOB格式时遇到问题。我知道有不同的方式来保存图像,但对于这个项目,我需要这样做。。。。以下是我正在尝试使用的代码:jQuery通过PHP上传图像并将其插入MySQLi BLOB,php,jquery,mysql,insert,blob,Php,Jquery,Mysql,Insert,Blob,我在将图像上传到MySQL并将其保存为BLOB格式时遇到问题。我知道有不同的方式来保存图像,但对于这个项目,我需要这样做。。。。以下是我正在尝试使用的代码: <html> <head> <script src="http://code.jquery.com/jquery-1.11.3.js"></script> <script type="text/javascript"> $(document).ready(functio
<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#submit_profile').on('click', function() {
$.ajax({
type: "POST",
url: 'updateProfile.php',
dataType: "html",
data: {
//that way it just dont work
//I've seen some examples where they use
// var formData = new FormData($('#updateProfile'));
userID: <?php echo $_SESSION['userID']; ?>,
image: $('#image').val(), // This didn't work
city: $('select[name=city]').val(),
desc: $('#desc').val()
},
success: function(data) {
$('.resultDiv').html(data);
}
)};
)};
)};
</script>
</head>
<body>
<form method="POST" id="updateProfile" enctype="multipart/form-data">
Image: <input type="file" name="image" id="image" accept="image/*"><br>
City: <select name="city">
<option value="Moscow">Moscow</option>
<option value="New York">New York</option>
</select>
Description: <textarea name="desc" id="desc"></textarea>
</form>
<button id="submit_profile" class="submit">Update</button>
</body>
</html>
$(文档).ready(函数(){
$('submit#u profile')。在('click',function()上{
$.ajax({
类型:“POST”,
url:'updateProfile.php',
数据类型:“html”,
数据:{
//那样就不行了
//我见过一些他们使用的例子
//var formData=new formData($(“#updateProfile”);
用户ID:,
image:$('#image').val(),//这不起作用
城市:$('select[name=city]')。val(),
desc:$('#desc').val()
},
成功:功能(数据){
$('.resultDiv').html(数据);
}
)};
)};
)};
图像:
城市:
莫斯科
纽约
说明:
更新
PHP方面:
<?php
session_start();
require ("db.php");
if($_POST['userID'] == $_SESSION['userID']) {
$userID = $_POST['userID'];
// Image
$city = trim(mysqli_escape_string($conn, $_POST['city']));
$desc = trim(mysqli_escape_string($conn, $_POST['desc']));
$errorinfo = $_FILES["userImage"]["error"];
$filename = $_FILES["userImage"]["name"];
$tmpfile = $_FILES["userImage"]["tmp_name"];
$filesize = $_FILES["userImage"]["size"];
$filetype = $_FILES["userImage"]["type"];
//
if (!($filetype == "image/jpeg" && $filesize > 0)) {
echo "<script>alert('Import of photo failed')</script>";
} else if($filesize < 0 && $filesize > 5120) {
echo "<script>alert('Import of photo failed!')</script>";
} else {
// Update query
}
} else {
echo "<script>window.location='/index.php';</script>";
}
?>
1。获取文件的内容。2.把它放到数据库里。3.返回到#1,将文件存储为文件,只需将文件的位置放在数据库中即可。我知道这种方法,但我需要通过Ajax调用将图像以BLOB格式存储在MySQL中。BLOB不是一种格式,它是一种字段类型,表示任意二进制数据存储在其中。我现在让你们参考我最初评论中的数字1和2。这是否有助于Ajax上传带有object.1的文件。获取文件的内容。2.把它放到数据库里。3.返回到#1,将文件存储为文件,只需将文件的位置放在数据库中即可。我知道这种方法,但我需要通过Ajax调用将图像以BLOB格式存储在MySQL中。BLOB不是一种格式,它是一种字段类型,表示任意二进制数据存储在其中。我现在让你们看一下我最初评论中的数字1和2。这是否有助于Ajax使用object上传文件。