使用mysqli将二进制文件从php发送到mysql
我想从php脚本将数据发送到两个表中,使用mysqli将二进制文件从php发送到mysql,php,mysql,mysqli,Php,Mysql,Mysqli,我想从php脚本将数据发送到两个表中,pic和idcard是mysql数据库中的两个二进制文件,它们都具有LONGBLOB类型始终成功执行第一个插入查询(插入user表),但第二个查询(插入“申请人”表)有两种情况: 1-如果我使pic和idcard字段在db中为空,则成功插入除这两个字段之外的其他数据 2-如果我使pic和idcard字段不可为空,则不在“申请人”表中插入任何内容 这两种情况我都无法将二进制数据保存到数据库中,问题出在哪里 我在$_FILES数组中也没有问题,通过传递它们,我可
pic
和idcard
是mysql数据库中的两个二进制文件,它们都具有LONGBLOB
类型始终成功执行第一个插入查询(插入user
表),但第二个查询(插入“申请人”表)有两种情况:
1-如果我使pic
和idcard
字段在db中为空,则成功插入除这两个字段之外的其他数据
2-如果我使pic
和idcard
字段不可为空,则不在“申请人”表中插入任何内容
这两种情况我都无法将二进制数据保存到数据库中,问题出在哪里
我在$_FILES数组中也没有问题,通过传递它们,我可以在将这些文件发布到php脚本时看到它们的详细信息。
我认为发送长时间的数据有问题,但我以前使用过它,就像现在一样
以下是脚本:
require_once('db_connection_config.php');
if (mysqli_connect_errno()) {
print_r("<div class='bs-example center-block'><div id='alertshow' class='bs-example center-block alert alert-danger'><a href='#' class='close' data-dismiss='alert'>×</a><strong>Connection Error:</strong>" . mysqli_connect_errno() . "</div></div>");
exit();
}
$today = date('Y:m:d');
$pass = randomPassword();
$stmt = $mysqli->prepare("INSERT INTO user (password,gender,firstname,lastname,email,phoneNum,mobileNum,Address,UserRoles_userRoleId) VALUES (?,?,?,?,?,?,?,?,3)");
$stmt->bind_param("sissssss", $pass, $_POST['gender'], $_POST['name'], $_POST['lastname'], $_POST['email'], $_POST['phonenum'], $_POST['mobilenum'], $_POST['address']);
$stmt->execute();
$mkey = mysqli_insert_id($mysqli);
$stmt->close();
if(isset($_FILES['pic']) && isset($_FILES['idcard'])){
$pic_file_path = $_FILES['pic']['tmp_name'];
if ( !file_exists($pic_file_path) ) {
throw new Exception('File not found.');
}
$pic_handle = fopen($pic_file_path, "rb");
if ( !$pic_handle ) {
throw new Exception('File open failed.');
}
$pic_content = null;
$idpic_file_path = $_FILES['idcard']['tmp_name'];
$idpic_handle = fopen($idpic_file_path, "rb");
$idpic_content = null;
$stmt = $mysqli->prepare("INSERT INTO applicant (userId,nationalCode,fatherName,birthPlace,nationality,religion,postalCode,picture,idpicture,registrationDate,militaryServiceStatus) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
$stmt->bind_param("isssiisbbss", $mkey, $_POST['nationalcode'], $_POST['fathername'], $_POST['birthcity'], $_POST['nationality'], $_POST['religion'], $_POST['postalcode'], $pic_content, $idpic_content, $today, $_POST['mss']);
while (!feof($pic_handle)) {
$stmt->send_long_data(1, fread($pic_handle, 8192));
}
fclose($pic_handle);
while (!feof($idpic_handle)) {
$stmt->send_long_data(1, fread($idpic_handle, 8192));
}
fclose($idpic_handle);
$stmt->execute();
$stmt->close();
}
}
function randomPassword() {
$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
$pass = array(); //remember to declare $pass as an array
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < 8; $i++) {
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
return implode($pass); //turn the array into a string
}
require_once('db_connection_config.php');
if(mysqli\u connect\u errno()){
打印(“连接错误:”.mysqli\u connect\u errno());
退出();
}
$today=日期('Y:m:d');
$pass=randomPassword();
$stmt=$mysqli->prepare(“插入用户(密码、性别、名字、姓氏、电子邮件、phoneNum、mobileNum、地址、用户角色和用户角色ID)值(?,,,,,,,,,,,,,,,,,?,3)”;
$stmt->bind_-param(“sissss”、$pass、$$u-POST['gender']、$$u-POST['name']、$\u-POST['lastname']、$\u-POST['email']、$\u-POST['phonenum']、$\u-POST['mobilenum']、$\u-POST['address']);
$stmt->execute();
$mkey=mysqli\u insert\u id($mysqli);
$stmt->close();
如果(isset($_文件['pic'])和&isset($_文件['idcard'])){
$pic_file_path=$_FILES['pic']['tmp_name'];
如果(!file_存在($pic_file_路径)){
抛出新异常(“未找到文件”);
}
$pic_handle=fopen($pic_file_path,“rb”);
如果(!$pic\u句柄){
抛出新异常(“文件打开失败”);
}
$pic_content=null;
$idpic_file_path=$_FILES['idcard']['tmp_name'];
$idpic_handle=fopen($idpic_文件_路径,“rb”);
$idpic_content=null;
$stmt=$mysqli->prepare(“插入申请人(用户名、国家代码、父亲姓名、出生地、国籍、宗教、邮政编码、图片、idpicture、注册日期、军人身份)值(?,,,,,,,,,,,,,,,,,,)”;
$stmt->bind_param(“Isssiisbbs”、$mkey、$POST['nationalcode']、$POST['fathername']、$POST['birthcity']、$POST['national']、$POST['religation']、$POST['postalcode']、$pic内容、$idpic内容、$today、$POST['mss');
而(!feof($pic_handle)){
$stmt->send_long_数据(1,fread($pic_handle,8192));
}
fclose($pic_handle);
而(!feof($idpic_handle)){
$stmt->send_long_data(1,fread($idpic_handle,8192));
}
fclose($idpic_handle);
$stmt->execute();
$stmt->close();
}
}
函数randomPassword(){
$alphabet=“abcdefghijklmnopqrstuwxyzabcdefghijklmnopqrstuwxyzo123456789”;
$pass=array();//记住将$pass声明为数组
$alphaLength=strlen($alphabet)-1;//将长度-1放入缓存
对于($i=0;$i<8;$i++){
$n=兰特(0,$alphaLength);
$pass[]=$alphabet[$n];
}
return introde($pass);//将数组转换为字符串
}
尽可能避免在数据库中存储文件。一个非常好的方法是将文件存储在某个文件夹中,并在数据库中存储文件本身的路径。“为什么错误源应该是这样的”-为什么不应该是这样?你能证明不是这样吗?是的,你可以-使用错误处理代码-这是一个很好的主意,当你寻找一个错误,你不知道它发生在哪里。添加错误处理。将文件数据放入nationalCode字段(1)为什么$\u POST['nationalCode']
而不是null
?发送长\u数据的第一个参数必须分别为7和8,而不是1。