问题
i have a form where the user inputs his info and has the ability to insert more than 1 longblob file (either an image or another file such as pdf), the insertion isn't working yet, but i'm able to manually insert file in MySQL and retrieve it ( View / download ), the attachment is to be uploaded to an "Attachment" Table and into a column "attachment" in Customer table, here's a code snippet of what is achieved:
<?php function insertFile_db($myFile,$conn,$tablename,$id){
$fileCount = count($myFile["name"]);
$sql="INSERT INTO Attachments (tableName,CustomerId,Name,File,Size,Type) ";
for ($i = 0; $i < $fileCount; $i++) {
$fileName=$myFile["name"][$i]; // Note : $myFile=$_FILES['Attachment']
$fileTmp_name=$myFile["tmp_name"][$i];
$fileType=$myFile["type"][$i];
$fileSize=$myFile["size"][$i];
//$fileContent = addslashes(file_get_contents($myFile['images']['tmp_name']));
$file=fopen($fileTmp_name,'r'); //'r' open for reading only, pts at the beginning of the file
$fileContent=fread($file,$fileSize);
fclose($file);
$fileContent = addslashes($fileContent);
if(!get_magic_quotes_gpc())
$fileName = addslashes($fileName);
if( $i==($fileCount-1) && $i==0 )
$sql.="VALUES ('".$tablename."', ".$id.", '".$fileName."','$fileContent','".$fileSize."','".$fileType."')" ;
else if($i!=($fileCount-1) && $i==0)
$sql.="VALUES ('".$tablename."', ".$id.", '".$fileName."','$fileContent','".$fileSize."','".$fileType."')," ;
else if($i==($fileCount-1) && $i!=0 )
$sql.=" ('".$tablename."', ".$id.", '".$fileName."','$fileContent','".$fileSize."','".$fileType."')" ;
else
$sql.=" ('".$tablename."', ".$id.", '".$fileName."','$fileContent','".$fileSize."','".$fileType."')," ;
}
if(!$conn->query($sql))
return True;
return Flase;
} ?>
and i'm calling the function :
if(isset($_FILES['Attachment']) && !empty($_FILES['Attachment']['name'][0])){
$last_id = $conn->insert_id;
$error=insertFile_db($_FILES['Attachment'],$conn,CONTNAME,$last_id);
if($error===True)
{$conn->close();header("location:/skylite/".CONTNAME."/Create?error=Internal error");exit();}
}
Now i'm focusing on insertion in Attachment table, i'd appreciate some tips and help!
回答1:
After checking for errors with mysqli_error(), the main problem was a "Date" field in my database that didn't have a default value, therefore the query wasn't working, posting this in case someone else had such a problem, this was one solution!
来源:https://stackoverflow.com/questions/47325447/multiple-blob-file-insertion-php