我知道将图像存储在mysql数据库中并不是一个好主意,但我只想尝试一下。
在mysql中我创建了这个表:CREATE TABLE tbl_images (
id tinyint(3) unsigned NOT NULL auto_increment,
image mediumblob NOT NULL,
PRIMARY KEY (id)
);
这是php代码:
if(isset($_FILES['image']) && $_FILES['image']['size'] > 0) {
$tmpName = $_FILES['image']['tmp_name'];
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);
$query ="INSERT INTO tbl_images(image)VALUES('".$data."')";
$results=mysql_query($query) or die(mysql_error());
$num=mysql_num_rows($results);
if($num>0)
print "Thank you, your file has been uploaded.";
}
else {
print "No image selected/uploaded";
}
?>
我在mysta的sytax中有错误,这里是输出:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in ...\insertimg.php on line 21
<html>
<title>Image</title>
<!--mikhaim ie form me3 upload ghabli benevisim-->
<!-- inbar mikhaim 2 database image ra gharar dahim-->
<form enctype="multipart/form-data" action="insertimg.php" method="post" name="changer">
<input name="MAX_FILE_SIZE" value="102400" type="hidden">
<input name="image" id="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">
</form>
</html>
答案 0 :(得分:1)
你的Else支架没有关闭.....
答案 1 :(得分:0)
在INSERT查询上检查num_rows没有意义。如果您想在错误发生时打印错误,请执行:mysql_query(...) or die(mysql_error())
。
编辑:我真的不明白你的INSERT查询为什么会失败,所以你可以删除所有的错误检查,然后在查询后打印“谢谢”。
编辑:这是它应该是什么样子:
<?php
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {
$tmpName = $_FILES['image']['tmp_name'];
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);
$query = "INSERT INTO tbl_images (image) VALUES ('$data')";
mysql_query($query) or die(mysql_error());
print "Thank you, your file has been uploaded.";
else {
print "No image selected/uploaded";
}
答案 2 :(得分:0)
确保记录正在插入,如果插入正常,则可以使用
$num = msql_affected_rows();
或
$num = mysql_insert_id();
而不是$num=mysql_num_rows($results);