我的SQL查询中存在一些错误,用于将图像插入MySQL数据库

时间:2016-05-19 10:43:22

标签: php html mysql sql

我的SQL查询中有一个错误,用于在MySQL数据库中插入图像。

当我尝试保存图像时,在SQL查询中出现插入图像的错误。错误是这个

  

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在#2; 2附近使用正确的语法(。?/ 279<<

<!DOCTYPE html>
<html>
<body>

    <form action="http://localhost/upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
      <input type="file" name="image" id="fileToUpload">
      <input type="submit" value="Upload Image" name="submit">
    </form>

</body>
</html>
<?php
  mysql_connect("localhost","root","") or die("not conected");
  mysql_select_db("image") or die("not selected");

  $file_name=$_FILES['image']['name'];
  $myfile=file_get_contents($_FILES['image']['tmp_name']);
  $q="INSERT INTO imagee(image_id,image_name,image)
                  VALUES('','$file_name','$myfile')";
  mysql_query($q) or die(mysql_error());
?>

2 个答案:

答案 0 :(得分:1)

<form action="phpfilename.php" method="post" 
       enctype="multipart/form-data">

<input  type="file" name="file" id="file" />	
<input type="submit" value="Upload Image" name="submit">

	

$file=$_FILES['file']['name'];
$dest="uploads/$file";
$src=$_FILES['file']['tmp_name'];
move_uploaded_file($src,$dest);

$result=mysql_query("insert into tablename(dbfieldname) values('$dest')");

答案 1 :(得分:0)

echo $myfile;

了解这些数据的外观。

你可能想要

base64_encode(file_get_contents($_FILES['image']['tmp_name'])) 

在使用INSERT查询之前。

http://php.net/manual/en/function.base64-encode.php