sheets()); for ($i = 0; $i < 1; $i++) { $Reader->ChangeSheet($i); $j = 0; foreach ($Reader as $Row) { if ($j == 0) { $j = 1; continue; } error_log("CURRENT ROW-> " . print_r($Row, true)); error_log("again here"); $emp_code = $Row[0]; $chronic_illness = trim($Row[2]); $dig_date = getDateInDbFormat($Row[3]); $med_code_id = getFieldFromTable('item_id', 'tbl_items', 'item_code', trim($Row[4])); $freq_id = getFieldFromTable('frequency_id', 'medicine_frequency', 'medicine_frequency', trim($Row[6])); $values = get_update_chronic_id($chronic_illness); error_log(" chronic ids " . $values . " item ids " . $med_code_id . " freq id " . $freq_id); $rid = 0; $rid = get_rid($emp_code, $values, $med_code_id, $freq_id); update_response_details($rid, $values, $emp_code, $dig_date, $med_code_id, $freq_id); } } function get_update_chronic_id($val) { $values = ''; $chronic = get_chronic_id($val); if ($chronic > 0) { $values = $chronic; } else { $insert = "insert into abnormality set abnormality_name='" . trim($val) . "'"; error_log("query " . $insert); $r_insert = mysqli_query($GLOBALS['conn'], $insert); $chronic = get_chronic_id($val); $values = $chronic; } return $values; } function get_chronic_id($chronic) { $sql = "select abnormality_id from abnormality where abnormality_name = '" . trim($chronic) . "'"; error_log("query " . $sql); $result = mysqli_query($GLOBALS['conn'], $sql); $row = mysqli_fetch_assoc($result); return $row['abnormality_id']; } function update_response_details($rid, $values, $emp_code, $dig_date, $item_id, $freq_id) { error_log("for rid " . $rid); if (($rid == 0 || $rid == '') && ($item_id == 0 || $item_id == '')) { $sql = "insert into prescription_master set diseases='" . $values . "', emp_id = (select id from patient_master where emp_code = '" . $emp_code . "') , diagnosis_date=STR_TO_DATE('" . $dig_date . "','%Y-%m-%d') ,ohc_type_id='" . $_SESSION['current_ohcttype'] . "'"; } else if ($rid > 0 && ($item_id == 0 || $item_id == '')) { $sql = "update prescription_master set diseases='" . $values . "', emp_id = (select id from patient_master where emp_code = '" . $emp_code . "') , diagnosis_date=STR_TO_DATE('" . $dig_date . "','%Y-%m-%d') ,ohc_type_id='" . $_SESSION['current_ohcttype'] . "' where prescription_id='" . $rid . "'"; } else if (($rid == 0 || $rid == '') && ($item_id > 0)) { $sql = "insert into prescription_master set diseases='" . $values . "', emp_id = (select id from patient_master where emp_code = '" . $emp_code . "') , diagnosis_date=STR_TO_DATE('" . $dig_date . "','%Y-%m-%d'), medicine_name='" . $item_id . "', medicine_frequency='" . $freq_id . "' ,ohc_type_id='" . $_SESSION['current_ohcttype'] . "'"; } else if ($rid > 0 && $item_id > 0) { $sql = "update prescription_master set diseases='" . $values . "', emp_id = (select id from patient_master where emp_code = '" . $emp_code . "') , diagnosis_date=STR_TO_DATE('" . $dig_date . "','%Y-%m-%d') , medicine_name='" . $item_id . "', medicine_frequency='" . $freq_id . "' ,ohc_type_id='" . $_SESSION['current_ohcttype'] . "' where prescription_id='" . $rid . "'"; } error_log("query " . $sql); if (!$result = mysqli_query($GLOBALS['conn'], $sql)) { error_log("error in inserting record " . mysqli_error($GLOBALS['conn']) . " query " . $sql); } } function get_rid($emp_code, $chronic, $item_id, $freq_id) { if ($item_id == 0 || $item_id == '') { $check = "select prescription_id from prescription_master where emp_id = (select id from patient_master where emp_code = '" . $emp_code . "') and diseases='" . $chronic . "'"; } else if ($item_id > 0) { $check = "select prescription_id from prescription_master where emp_id = (select id from patient_master where emp_code = '" . $emp_code . "') and diseases='" . $chronic . "' and medicine_name='" . $item_id . "'"; } error_log("query " . $check); $res = mysqli_query($GLOBALS['conn'], $check); $rc = mysqli_fetch_assoc($res); error_log("full data " . print_r($rc, true)); return $rc['prescription_id']; }