php - 上传图片到mysql数据库失败

标签 php mysql image upload

嘿,我有以下代码:

index.php

select.php

您还可以在 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/

相关文章:

php - 在浏览器上显示 .docx (.doc) 而无需在 php 中下载

javascript - 尝试使用ajax调用php函数不起作用

php - opencart 检测移动设备并重定向到移动主题

Android从图库上传图片到服务器

javascript - 如何拍摄 <img> 视频流的照片? HTML5 JavaScript

php - 显示有关某个项目的更多信息 php codeigniter

php - mysql php字符集设置

mysql - 有人可以向我解释一下这个 mysql select 查询值的逻辑吗?

mysql - 将 MySQL 数据库转储转换为 SQLite - INSERT 语法

css - 背景图片没有真正响应