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];
}
}
/* Extract cho phone. */
elseif($row == 12){
$cho[$data[7]] = $data[8];
}
/* Insert laboratory/personnel info and generate register keys. */
elseif($row == 14){
for($index=0; $index < count($data); $index++){
/* Initiate keys with null values. */
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.
Bind and insert pi data. */
$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; /* Extract generated pid. */
}
/* In case of duplicate entry. */
else{
$email = $pi['piemail'];
/* Extract id by email. */
if ($person = $db->prepare("SELECT pid FROM Person WHERE email=?")){
/* Bind selector and execute. */
$person->bind_param("s",$email);
if($person->execute()){
/* Bind result variables and fetch. */
$person->bind_result($pi);
$person->fetch();
}
}
}
}
/* Bind and insert cho data. */
$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; /* Extract generated pid. */
}
/* In case of duplicate entry. */
else{
$email = $cho['choemail'];
/* Extract id by email. */
if ($person = $db->prepare("SELECT pid FROM Person WHERE email=?")){
/* Bind selector and execute. */
$person->bind_param("s",$email);
if($person->execute()){
/* Bind result variables and fetch. */
$person->bind_result($cho);
$person->fetch();
}
}
}
}
$person->close();
/* lab entry
Bind and insert lab data. */
$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();
$lab = $db->insert_id; /* Extract generated lid. */
}
}
/* Extract register data by row. */
elseif($row > 15){
$d = 0; /* Reset index. */
foreach($register as $key=>&$value){
/* Replace null entries with identifiable string. */
if($data[$d]){
$value = $data[$d];
}
else{
$value = $null;
}
$d++;
/* Avoid segmentation fault. */
if($d >= count($register)){
break;
}
}
/* inventory entries
Bind and insert inventory data. */
$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);
/* Query failed, display error. */
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 . "
";
}
/* Track number of successful entries. */
else{
$success++;
$inventory->close();
}
}
}
}
$row++; /* Update row. */
}
/* Close all remaining connections/streams. */
fclose($fhandle);
echo "
FILE: " . $file . "
";
echo "PI id: " . $pi . "
";
echo "CHO id: " . $cho . "
";
echo "LAB id: " . $lab . "
";
echo $success . " successful entries." . "
";
}
}
/* Close db connection. */
$db->close();
?>