我想用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行
请指教...
请分享你的数据库结构 –
好吧..已经分享。 –
尝试打印echo mysql_errno($ dbconn)。 “:”。 mysql_error($ dbconn)。 “\ n” 个; –