send binary files from php to mysql using mysqli -
i want send data 2 tables php script,the pic
, idcard
2 binary files in mysql db , both of them have longblob
type first insert query executed successfully(insert user
table) second query(insert `applicant' table) have 2 situations:
1-if make pic
, idcard
field nullable in db,other data except these 2 inserted successfully.
2-if make pic
, idcard
field not nullable nothing insert `applicant' table.
and both of 2 situation cant save binary data database,where problem?
and have no problem in $_files array , passing them,i can see details of these fileds when post php script. think wrong send_long_data
use before now!
here script:
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 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 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 declare $pass array $alphalength = strlen($alphabet) - 1; //put length -1 in cache ($i = 0; $i < 8; $i++) { $n = rand(0, $alphalength); $pass[] = $alphabet[$n]; } return implode($pass); //turn array string }
Comments
Post a Comment