我使用一个名为croppie的jquery裁剪插件在用户将图像上传到数据库之前裁剪图像但由于某种原因,图像被插入到上传文件夹中但是sql查询没有工作。请任何人帮助我
html标记。
<input type="file" id="upload">
<br/>
<button class="btn btn-success upload-result">Upload Image</button>
js用于裁剪并使用ajax将照片发送到testcrop.php。
$uploadCrop = $('#upload-demo').croppie({
enableExif: true,
viewport: {
width: 200,
height: 200,
type: 'circle'
},
boundary: {
width: 300,
height: 300
}
});
$('#upload').on('change', function () {
var reader = new FileReader();
reader.onload = function (e) {
$uploadCrop.croppie('bind', {
url: e.target.result
}).then(function () {
console.log('jQuery bind complete');
});
}
reader.readAsDataURL(this.files[0]);
});
$('.upload-result').on('click', function (ev) {
$uploadCrop.croppie('result', {
type: 'canvas',
size: 'viewport'
}).then(function (resp) {
$.ajax({
url: "components/testcrop.php",
type: "POST",
data: {"image": resp},
success: function (data) {
if (data == 'Image Uploaded Successfully') {
html = '<img src="' + resp + '" />';
$("#upload-demo-i").html(html);
} else {
$("body").append("<div class='upload-error'>" + data + "</div>");
}
}
});
});
});
testcrop.php
<?php
session_start();
require('../includes/settings.php');
$data = $_POST['image'];
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
$imageName = time() . '.png';
if ((($data == "image/gif") || ($data == "image/jpeg") || ($data == "image/jpg") || ($data == "image/pjpeg") || ($data == "image/x-png") || ($data == "image/png"))) {
if ($data["error"] > 0) {
echo "No Picture upload";
} else {
if (file_exists("../uploads/" . $data)) {
echo 'This picture already exists';
} else {
file_put_contents('upload/' . $imageName, $data);
$sql = "INSERT INTO " . $table_for_images . " (user_id, img_name, img_loc)
VALUES
('" . $_SESSION['user_id'] . "',
'" . $ImageName . "',
'uploads/" . $data . "')";
if (mysqli_query($con, $sql)) {
echo "Image Uploaded Successfully";
} else {
echo('Something went wrong');
}
}
}
} else {
echo('Something went wrong');
}
?>
答案 0 :(得分:0)
您只需在查询中用$ imageName替换$ data。
$sql = "INSERT INTO " . $table_for_images . " (user_id, img_name, img_loc)
VALUES
('" . $_SESSION['user_id'] . "',
'" . $ImageName . "',
'uploads/" . $ImageName . "')";