图像未插入mysql。来自Ajax FAIL的回应即将来临。
代码如下
<?php
include "db.php";
//keep your db name
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
//you keep your column name setting for insertion. I keep image type Blob.
$query = "INSERT INTO images (id,image) VALUES(0,'$image')";
$qry = mysqli_query($con,$query);
if($qry){
echo "success";
}else{
echo "fail";
}
?>
<html lang="en">
<head>
<title>PHP - Image Uploading with Form JS Example</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script>
$(document).ready(function() {
$(".upload-image").click(function(){
$(".form-horizontal").ajaxForm({target: '.preview'}).submit();
});
});
</script>
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">PHP - Image Uploading with Form JS Example</a>
</div>
</div>
</nav>
<div class="container">
<form action="ajax_php_file.php" enctype="multipart/form-data" class="form-horizontal" method="post">
<div class="preview"></div>
<input type="file" name="image" class="form-control" style="width:30%" />
<button class="btn btn-primary upload-image">Save</button>
</form>
</div>
</body>
</html>
表==== 这是表格
DROP TABLE IF EXISTS live_your_life
。images
;
CREATE TABLE live_your_life
。images
(
id
int(10)unsigned NOT NULL DEFAULT'0',
image
blob,
主要关键(id
)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
任何帮助将不胜感激!!
答案 0 :(得分:0)
您应该在表格中为主键添加自动增量 我不建议您将图像插入数据库,但如果您坚持将图像插入数据库,则应确保先将图像上传到工作区,例如:path / upload /
然后当您插入时,您可以使用
LOAD_FILE('/some/path/image.png')
ex:INSERT INTO table_name (ID,IMAGE) VALUES(1,LOAD_FILE('your upload path'));
注意:如果您在工作区路径中上传图像,然后将路径保存在数据库中,那将是很好的,因此当您需要图像时,可以从工作区文件夹加载它而不是从数据库加载
抱歉英语不好