doukezi4576 2015-07-10 03:41
浏览 61

无法将图片上传到mysql数据库使用php

I am trying to upload a image to MySQL databases using php5 script. And I am receiving an notice error.

Error, query failed

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)
{
         $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
 
          $query = "INSERT INTO img_tbl (img_name, img_type, img_size, img_data )
                        VALUES ('$fileName', '$fileType', '$fileSize', '$imgContent')";
              
          mysql_query($query) or die('Error, query failed'.mysql_errno($dbconn) . ": " . mysql_error($dbconn) . "
");
          $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");
?>

I have upload an image file but still have error on it.

But now I have counter another error for view 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']))
{


    $id    = $_REQUEST ['id'];
    $query = "SELECT img_name, img_type, img_size, img_data FROM img_tbl WHERE id = ‘$id’";

    $result = mysql_query($query) or die(mysql_error());
    list($name, $type, $size, $content) = mysql_fetch_array($result);

    header("Content-length: $size");
    header("Content-type: $type");
    print $content;

    mysql_close($dbconn);
}
?>

The error code:

Notice: Undefined variable: id� in C:\xampp\htdocs\sandbox\Testing\uploadImage2_viewimage.php on line 12 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '�' at line 1

Please advise...

</div>
  • 写回答

3条回答 默认 最新

  • doufan9377 2015-07-10 03:47
    关注

    Remove the ' ' from table fields in query .use this query :

    $query = "INSERT INTO img_tbl (img_name, img_type, img_size, img_data )
                                VALUES ('$fileName', '$fileType', '$fileSize', '$imgContent')";
    

    also please start to use PDO or mysqli as your query is open for sql injection

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)