多个Blob文件插入php [英] multiple blob file insertion php
问题描述
我有一个表格,用户可以输入自己的信息,并且可以插入多个1个longblob文件(图像或其他文件,例如pdf),但该插入尚不起作用,但是我可以在MySQL中手动插入文件并进行检索(查看/下载),附件将上传到附件"表中,并上传到客户"表中的附件"列中,这是实现的代码段:
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;
} ?>
我正在调用该函数:
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!
推荐答案
使用mysqli_error()检查错误后,主要问题是数据库中的日期"字段没有默认值,因此查询无法正常工作,以防其他人遇到此类问题,将其发布,这是一种解决方案!
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!
这篇关于多个Blob文件插入php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!