[英]Uploading blob type images to database, my code says it is uploading the image but not being stored in the database
這是我下面的代碼,頂部的HTML表單是上傳圖像的簡單表單,PHP中的SQL命令用於上傳至數據庫。 我遇到的問題是它似乎正在訪問if(substr($ imageType,0,5)==“ image”)並將“ Image Uploaded”輸出到瀏覽器,但實際上並未將此圖像上傳到我的數據庫。
我的數據庫表名為blob,具有三個字段; id(int(11)以及自動遞增和主鍵),名稱(varchar(30))和圖像(blob)。
編輯:我嘗試了由米勒建議的反引號,但是沒有運氣,使用mysql_error()給了我這個錯誤
警告:mysql_error()期望參數1為資源,在第31行的C:\\ wamp \\ www \\ Box \\ index.php中給出布爾值
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Image Upload</title>
</head>
<body>
<form action="index.php" method ="POST" enctype="multipart/form-data">
<input type="file" name="image" /><input type="submit" name="submit" value="Upload" />
</form>
<?php
if(isset($_POST['submit']))
{
mysql_connect("localhost", "root", "root");//password root
mysql_select_db("content");//db name content
$imageName = mysql_real_escape_string($_FILES["image"]["name"]);//provides protection against injection
$imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
$imageType = mysql_real_escape_string($_FILES["image"]["type"]);
if(substr($imageType,0,5) == "image")
{
mysql_query("INSERT INTO 'blob' VALUES('','$imageName','$imageData')");
echo "Image Uploaded";
}
else
{
echo "Only images are allowed";
}
}
?>
</body>
</html>
與BarryDevSF的解決方案類似,將行更改為:
mysql_query("INSERT INTO `blob` VALUES(NULL,'$imageName','$imageData')");
代替:
mysql_query("INSERT INTO 'blob' VALUES('','$imageName','$imageData')");
解決了這個問題。
您正在將一個空字符串傳遞給INT ID。 如果沒有它,我會嘗試的。
代替:
mysql_query("INSERT INTO 'blob' VALUES('','$imageName','$imageData')");
嘗試:
mysql_query("INSERT INTO blob (name, image) VALUES('$imageName','$imageData')");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.