PHP脚本不会将BLOB映像插入数据库,而是插入文件的名称

问题描述 投票:-1回答:1

早上好!最近我尝试将图像插入数据库,以及其他String类型数据。它适用于String类型数据,但不适用于BLOB类型的图像。我注意到只插入了图像的名称,而不是图像。我是PHP脚本和SQL数据库的初学者。有谁能够帮我?:)

    $fname = mysqli_real_escape_string($connect, $_POST['yname']);
    $lname = mysqli_real_escape_string($connect, $_POST['email']);
      $age = mysqli_real_escape_string($connect, $_POST['name']);
       $url = mysqli_real_escape_string($connect, $_POST['url']);
       $color1 = mysqli_real_escape_string($connect, $_POST['color1']);
      $color2 = mysqli_real_escape_string($connect, $_POST['color2']);
    $contact1 = mysqli_real_escape_string($connect, $_POST['contact1']);
     $contact2 = mysqli_real_escape_string($connect, $_POST['contact2']);
     $contact3 = mysqli_real_escape_string($connect, $_POST['contact3']);
         $contact4 = mysqli_real_escape_string($connect, $_POST['contact4']);
          $bio = mysqli_real_escape_string($connect, $_POST['message']);
           $farewell = mysqli_real_escape_string($connect, $_POST['farewell']);
          $ppname = mysqli_real_escape_string($connect, $_POST['ppname']);
             $password = mysqli_real_escape_string($connect, $_POST['password']);
               $dadress = mysqli_real_escape_string($connect, $_POST['dadress']);
             $version = "full";
    $filename = $_FILES['uploadfile']['name'];
  $filetmpname = $_FILES['uploadfile']['tmp_name'];
    $filename2 = $_FILES['uploadfile2']['name'];
  $filetmpname2 = $_FILES['uploadfile2']['tmp_name'];
  $folder = 'imagesuploadedf/';
 // edited and added below code. it will check if folder exists and create if not exists.....
 $foldername = 'imagesuploadedf';
 if ( ! is_dir($foldername)) {
     mkdir($foldername); }
 // end of edited and added code
  move_uploaded_file($filetmpname, $folder.$filename);
  move_uploaded_file($filetmpname2, $folder.$filename2);

// $sql = "INSERT INTO `uploadedimage` (`imagename`)  VALUES ('$filename')";
    // connect to mysql database using mysqli

    // mysql query to insert data
         $query = "INSERT INTO `tabela`(`name`, `url`, `email`, `imagename`, `imagename2`, `color1`, `color2`, `contact1`, `contact2`, `contact3`, `contact4`, `bio`, `farewell`, `ppname`, `version`) VALUES ('$fname', '$lname', '$age', '$filename', '$filename2', '$color1', '$color2', '$contact1', '$contact2', '$contact3', '$contact4', '$bio', '$farewell', '$ppname', '$version')";      
       $sql = "INSERT INTO `tabela`(`name`, `url`, `email`, `color1`, `color2`, `contact1`, `contact2`, `contact3`, `contact4`, `bio`, `farewell`, `ppname`, `version`, `dadress`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";    
  $stmt = mysqli_stmt_init($connect);
  if(!mysqli_stmt_prepare($stmt, $sql)){
      echo "Error";
  } else{
  mysqli_stmt_bind_param($stmt, "ssssssssssssss", $fname, $url, $lname, $color1, $color2, $contact1, $contact2, $contact3, $contact4, $bio, $farewell, $ppname, $version, $dadress);
  mysqli_stmt_execute($stmt);
  }
  $sql2 = "UPDATE tabela SET imagename = '$filename', imagename2 = '$filename2' WHERE ppname = '$ppname'";
   mysqli_query($connect,$sql2);
php mysql sql mysqli blob
1个回答
0
投票

如果您的BLOB字段是imagename,那么使用此代码获取图像数据,而不是使用$filename = $_FILES['uploadfile']['name']将图像名称分配给变量

$imageBlob = fopen($filetmpname, 'rb');

要么

$imageBlob = file_get_content($filetmpname);

然后使用此$imageBlob使用简单的更新查询存储在DB列中。

© www.soinside.com 2019 - 2024. All rights reserved.