6 and $row < 12){
/* avoid null keys */
if($data[0]){
$laboratory[$data[0]] = $data[1];
}
if($row < 10){
$pi[$data[7]] = $data[8];
}
else{
$cho[$data[7]] = $data[8];
}
}
/* cho phone */
elseif($row == 12){
$cho[$data[7]] = $data[8];
}
/* generate register keys */
elseif($row == 14){
for($index=0; $index < count($data); $index++){
if($data[$index]){
$register[$data[$index]] = "";
}
}
foreach($pi as $key=>&$value){
if(!$value){
$value = $null;
}
}
foreach($cho as $key=>&$value){
if(!$value){
$value = $null;
}
}
/* person entries */
$person = $db->stmt_init();
if($person = $db->prepare("INSERT INTO Person(pname,email,password,phone,privileges) VALUES (?,?,?,?,?)")){
$person->bind_param("sssss",$pi["pi"],$pi["piemail"],$null,$pi["piphone"],$null);
if($person->execute()){
$pi = $db->insert_id;
}
/* duplicate entry */
else{
$email = $pi['piemail'];
if ($person = $db->prepare("SELECT pid FROM Person WHERE email=?")){
$person->bind_param("s",$email);
if($person->execute()){
$person->bind_result($pi);
$person->fetch();
}
}
}
}
/* cho */
$person = $db->stmt_init();
if($person = $db->prepare("INSERT INTO Person(pname,email,password,phone,privileges) VALUES (?,?,?,?,?)")){
$person->bind_param("sssss",$cho["cho"],$cho["choemail"],$null,$cho["chophone"],$null);
if($person->execute()){
$cho = $db->insert_id;
}
/* duplicate entry */
else{
$email = $cho['choemail'];
if ($person = $db->prepare("SELECT pid FROM Person WHERE email=?")){
$person->bind_param("s",$email);
if($person->execute()){
$person->bind_result($cho);
$person->fetch();
}
}
}
}
$person->close();
/* lab entry */
$lab = $db->stmt_init();
if($lab = $db->prepare("INSERT INTO Laboratory(lname,lab,department,building,extension,pi,cho) VALUES (?,?,?,?,?,?,?)")){
$lab->bind_param("sssssii",$laboratory["lname"],$laboratory["lab"],$laboratory["department"],$laboratory["building"],$laboratory["extension"],$pi,$cho);
$lab->execute();
$lab->close();
/* extract generated lab_id */
$lab_id = $db->insert_id;
}
}
/* extract register data by row */
elseif($row > 15){
/* reset index */
$d = 0;
foreach($register as $key=>&$value){
if($data[$d]){
$value = $data[$d];
}
else{
$value = $null;
}
$d += 1;
/* avoid seg fault */
if($d >= count($register)){
break;
}
}
/* inventory entries */
$inventory = $db->stmt_init();
if($register['chemical'] != "null entry"){
if($inventory = $db->prepare("INSERT INTO Inventory(chemical,manufacturer,sds,cas,state,hazard,type,amount,quantity,total,location,ghs,lid) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)")){
$inventory->bind_param("ssssssssssssi",$register["chemical"],$register["manufacturer"],$register["sds"],$register["cas"],$register["state"],$register["hazard"],$register["type"],$register["amount"],$register["quantity"],$register["total"],$register["location"],$register["ghs"],$lab_id);
/* query failed */
if(!($inventory->execute())){
echo "Entry error:";
echo "
";
print_r($inventory);
echo "
";
print_r($register);
echo "
";
echo "PI id: " . $pi . "
";
echo "CHO id: " . $cho . "
";
echo "LAB id: " . $lab_id . "
";
}
/* successful entries */
else{
$success += 1;
$inventory->close();
}
}
}
}
/* update row */
$row += 1;
}
fclose($fhandle);
echo "
FILE: " . $file . "
";
echo "PI id: " . $pi . "
";
echo "CHO id: " . $cho . "
";
echo "LAB id: " . $lab_id . "
";
echo $success . " successful entries." . "
";
}
}
$db->close();
?>