2016-12-06 33 views
0

圖像沒有被插入到mysql中。來自Ajax FAIL響應即將到來。下面 我試圖通過Ajax插入圖像。但查詢沒有執行

<?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(){ 
 
      \t $(".form-horizontal").ajaxForm({target: '.preview'}).submit(); 
 
      }); 
 
     }); 
 
</script> 
 
</head> 
 
<body> 
 
\t <nav class="navbar navbar-default"> 
 
\t \t <div class="container-fluid"> 
 
\t \t <div class="navbar-header"> 
 
\t \t <a class="navbar-brand" href="#">PHP - Image Uploading with Form JS Example</a> 
 
\t \t </div> 
 
\t \t </div> 
 
\t </nav> 
 
\t <div class="container"> 
 
\t <form action="ajax_php_file.php" enctype="multipart/form-data" class="form-horizontal" method="post"> 
 
\t \t <div class="preview"></div> 
 
\t \t <input type="file" name="image" class="form-control" style="width:30%" /> 
 
\t \t <button class="btn btn-primary upload-image">Save</button> 
 
\t </form> 
 
\t </div> 
 
</body> 
 
</html>

表====

代碼給出這裏是表

DROP TABLE IF EXISTS live_your_lifeimages; CREATE TABLE live_your_lifeimagesid INT(10)無符號NOT NULL DEFAULT '0', image團塊, PRIMARY KEY(id) )ENGINE = InnoDB的默認字符集= LATIN1;

任何幫助將不勝感激!

+0

嘗試在你的表中添加自動增量: http://dev.mysql.com/doc/refman/5.7/en/example-auto-increment.html –

回答

0

您應該將自動增量添加到您的主鍵 我不建議您將圖像插入到數據庫中,但是如果您堅持要將圖像插入到數據庫中,則應確保將圖像上載到您的工作區中,例如:路徑/上傳/

然後當你插入你可以使用

LOAD_FILE('/some/path/image.png') 

例如:INSERT INTO table_name (ID,IMAGE) VALUES(1,LOAD_FILE('your upload path'));

注意:如果你上傳的圖片,然後在WOR這將是很好k-空間的路徑,然後在數據庫中保存的路徑,所以當你需要的圖像,可以從工作區文件夾無法從數據庫加載

遺憾的英語不好

+0

感謝您的幫助... –

相關問題