无法上传图像到mysql数据库使用php
我想用php5脚本将图像上传到MySQL数据库。我收到通知错误。无法上传图像到mysql数据库使用php
错误,查询失败
UploadImage.php
<?php
session_start();
?>
<HTML>
<HEAD>
<TITLE> Image Upload</TITLE>
</HEAD>
<BODY>
<FORM NAME="f1" METHOD="POST" ACTION="uploadImage2.php" ENCTYPE="multipart/form-data">
<table>
<tr><td> Image Upload Page </td></tr>
<tr><td> <input type="file" name="imgfile"/></td></tr>
<tr><td> <input type="submit" name="submit" value="Save"/> </td></tr>
</table>
</FORM>
</BODY>
</HTML>
UploadImage2.php
<?php
include "dbconfig.php";
$dbconn = mysql_connect($dbhost, $dbusr, $dbpass) or die("Error Occurred-".mysql_error());
mysql_select_db($dbname, $dbconn) or die("Unable to select database");
if(isset($_REQUEST['submit']) && $_FILES['imgfile']['size'] > 0)
{
\t \t $fileName = mysql_real_escape_string($_FILES['imgfile']['name']); // image file name
\t \t $tmpName = $_FILES['imgfile']['tmp_name']; // name of the temporary stored file name
\t \t $fileSize = mysql_real_escape_string($_FILES['imgfile']['size']); // size of the uploaded file
\t \t $fileType = mysql_real_escape_string($_FILES['imgfile']['type']); //
\t \t
\t \t
\t \t $fp = fopen($tmpName, 'r'); // open a file handle of the temporary file
\t \t $imgContent = fread($fp, filesize($tmpName)); // read the temp file
\t \t $imgContent = mysql_real_escape_string($imgContent);
\t \t fclose($fp); // close the file handle
$query = "INSERT INTO img_tbl (img_name, img_type, img_size, img_data)
VALUES ('$fileName', '$fileType', '$fileSize', '$imgContent')";
\t
mysql_query($query) or die('Error, query failed'.mysql_errno($dbconn) . ": " . mysql_error($dbconn) . "\n");
$imgid = mysql_insert_id(); // autoincrement id of the uploaded entry
//mysql_close($dbconn);
echo "<br>Image successfully uploaded to database<br>";
echo "<a href=\"uploadImage2_viewimage.php?id=$imgid\">View Image</a>";
}else die("You have not selected any image");
?>
我上传了一个图像文件,但仍然有错误。
但现在我有计数器查看图像的另一个错误。
<?php
// get the file with the id from database
include "dbconfig.php";
$dbconn = mysql_connect($dbhost, $dbusr, $dbpass) or die("Error Occurred-".mysql_error());
mysql_select_db($dbname, $dbconn) or die("Unable to select database");
if(isset($_REQUEST['id']))
{
\t $id = $_REQUEST ['id'];
\t $query = "SELECT img_name, img_type, img_size, img_data FROM img_tbl WHERE id = ‘$id’";
\t $result = mysql_query($query) or die(mysql_error());
\t list($name, $type, $size, $content) = mysql_fetch_array($result);
\t header("Content-length: $size");
\t header("Content-type: $type");
\t print $content;
\t mysql_close($dbconn);
}
?>
错误代码:
注意:未定义的变量:ID在C:\ XAMPP \ htdocs中\沙盘\测试\ uploadImage2_viewimage.php上线12 您有一个您的SQL语法错误;检查对应于你的MySQL服务器版本使用附近的“正确的语法手册”第1行
请指教...
删除“”从表中的字段查询。使用此查询:
$query = "INSERT INTO img_tbl (img_name, img_type, img_size, img_data)
VALUES ('$fileName', '$fileType', '$fileSize', '$imgContent')";
也请开始使用PDO或作为mysqli的查询是开放的SQL注入
嗨,我仍然有同样的错误..但无论如何感谢您的答案。 –
尝试正确我相信它会工作 –
嗨,我已尝试正确,但仍然是相同的错误..建议..并感谢您的帮助。 –
这应该工作:
$query = "
INSERT INTO `img_tbl`
(`img_name`, `img_type`, `img_size`, `img_data`)
VALUES
('".$fileName."', '".$fileType."', '".$fileSize."', '".$imgContent."')
";
谢谢但仍然有同样的错误。 –
检查我的更新 – john
似乎在$imgContent
一些特殊字符更是打破了查询字符串
请使用mysql_real_escape_string
发送到数据库
$fileName = mysql_real_escape_string($_FILES['imgfile']['name']); // image file name
$tmpName = $_FILES['imgfile']['tmp_name']; // name of the temporary stored file name
$fileSize = mysql_real_escape_string($_FILES['imgfile']['size']); // size of the uploaded file
$fileType = mysql_real_escape_string($_FILES['imgfile']['type']); //
$fp = fopen($tmpName, 'r'); // open a file handle of the temporary file
$imgContent = fread($fp, filesize($tmpName)); // read the temp file
$imgContent = mysql_real_escape_string($imgContent);
fclose($fp); // close the file handle
UPDATE
之前格式化数据如果第一个解决方案没有解决问题,请检查是否有问题重新设置NULL值,你有一些数据库列设置为NOT NULL。所以你不能向它们插入NULL值。
希望这会有所帮助:)
请分享你的数据库结构 –
好吧..已经分享。 –
尝试打印echo mysql_errno($ dbconn)。 “:”。 mysql_error($ dbconn)。 “\ n” 个; –