uploade image to mysql data base failure

时间:2017-04-09 08:34:41

标签: php mysql image upload

嘿,我有以下代码:

index.php

select.php

你也可以在GitHub(简单在线图书馆)中找到整个项目

哪个工作正常并且在mysql数据库中保存bookname bookauthor bookpages但它只是在数据库中保存图像名称和图像路径我只想将图像移动到名为images的新文件夹中并尝试几种方式,但它们都不适用于我。< / p>

1 个答案:

答案 0 :(得分:1)

应修复如下

 <?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();
}
});