嘿,我有以下代码:
您还可以在 GitHub 中找到整个项目(simple-online-library)
它工作正常,并在 mysql 数据库中保存书名 bookauthor 书页,但它只是在数据库中保存图像名称和图像路径,我只想将图像移动到名为 images 的新文件夹中,并尝试多种方法,但没有一个对我有用。
最佳答案
应按如下方式修复
<?php
require_once("db.php");
$file = addslashes(file_get_contents($_FILES["images"]["tmp_name"]));
$sql = "INSERT INTO test1(name, lastname, age , images) VALUES('".$_POST["name"]."', '".$_POST["lastname"]."', '".$_POST["age"]."', '".$file."')";
if(mysqli_query($connect, $sql))
{
echo 'infos saved';
}
?>
更新
您的 AJAX 脚本仅发送文件名,而不发送图像。正确的 JS 应该与下面类似。
$.ajax({
url: "ajax_php_file.php", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false
success: function(data) // A function to be called if request succeeds
{
// do something ex. $('#loading').hide();
}
});
关于php - 上传图片到mysql数据库失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43304706/