为什么在将图像上传到数据库时会出错?

时间:2018-09-15 10:24:13

标签: php html

我想使用PHP将图像上传到数据库。尝试上传时出现以下错误:

 lstCallAPI=['item1', 'item2', 'item3'];

        loadPropertyTypeData(){
        this._dataService.get('/api/product/get-add-property')
        .subscribe((response: any) => {
         for (let index = 0; index < response.result.length; index++) {
           const element = response.result[index];
          this.lstCallAPI.push(element.Name);
         }
          this.modalOverflow.show();
        });
      }
    public requestAutocompleteItems = (text: string): Observable<string[]> => {
      return of(this.lstCallAPI);
    };
Error: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

我以前已经设法连接到数据库,并且能够插入除图像内容以外的任何内容。这是我的代码:

dbname:img    
tablename:image   
column:img   
type:LongBLob.

3 个答案:

答案 0 :(得分:1)

出现此错误的原因是,您实际上是在获取文件的原始内容,然后将其转储到SQL语句中,而没有任何形式的清理或编码。

尝试:添加斜线

仅使用addslashes可以避免任何会导致SQL查询失败的冲突字符。

$F = file_get_contents($_FILES['file']['tmp_name']);
$data = addslashes($F);
$Q = "insert into image (img) values('$data')";

尝试:base64_encode

另一种选择是改为使用base64_encode

注意:使用此方法时,您的img列的类型应为TEXT

$F = file_get_contents($_FILES['file']['tmp_name']);
$encoded = base64_encode($F);
$Q = "insert into image (img) values('$encoded')";

从数据库中检索值时,您需要base64_decode才能再次获取原始数据。

答案 1 :(得分:0)

首先通过以下方法检查您是否与数据库连接:

$C = new mysqli("localhost","root","","img");
if ($C->connect_errno) {
  echo "Errno: " . $mysqli->connect_errno . "\n";
  echo "Error: " . $mysqli->connect_error . "\n";
}

答案 2 :(得分:0)

尝试一下

<form method="post" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit">Go</button>
</form>



<?php
$db_host = "localhost"; 
$db_username = "root";  
$db_pass = "";  
$db_name = "img"; 
$conn= mysqli_connect("$db_host","$db_username","$db_pass", "$db_name") or die ("could not connect to mysql");

if(isset($_FILES['file'])) {
$target_dir = "uploads/";//folder name to mave uploaded file
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$check = getimagesize($_FILES["file"]["tmp_name"]);
if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
} else {
    echo "File is not an image.";
    $uploadOk = 0;
}

// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["file"]["size"] > 20000000) {
echo "Sorry, your file is too large Max file size is 2.5MB.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" 
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {

    $image_name = basename( $_FILES["file"]["name"]);


 } else {
    $image_name = "default.jpg";
 }
 }
 mysqli_query($conn, "INSERT INTO image (img) VALUES('$image_name ')"")or die  (mysqli_error($conn));

 echo '<p align="center">Successful</p>';    

 }
 ?>