Data not inserting into the database table. but when var_dump it show all record to be inserted

we are getting somewhere.
let me run it i will get back to you in a second

here is my code

$title=$_POST['title'];
	$surname=$_POST['surname'];
	$firstname=$_POST['firstname'];
	$middle_name=$_POST['middle_name'];
	$gender=$_POST['gender'];
	$dob=$_POST['dob'];
	$marital_status=$_POST['marital_status'];
	$occupation=$_POST['occupation'];
	$occupation2=$_POST['occupation2'];
	$phone_number=$_POST['phone_number'];
	$monthly_gross=$_POST['monthly_gross'];
	$nhf=$_POST['nhf'];
	$nhis=$_POST['nhis'];
	$nsitf=$_POST['nsitf'];
	$basic_salary=$_POST['basic_salary'];
	$grade=$_POST['grade'];
	$designation=$_POST['designation'];
	$pension=$_POST['pension'];
	$gratuity=$_POST['gratuity'];
	$institution=$_SESSION['Company'];
	$UserLogin=$_SESSION['UserLogin'];
	$textfile_surname = $_POST['surname'];
	$textfile_occupation = $_POST['occupation2'];
	$textfile_occupation1 = $_POST['occupation'];
	$textfile__monthlygross = $_POST['monthly_gross'];

for($row=1; $row<=$highestRow; $row++)  
                { 
				
				
	
	$highestRowIndex = $worksheet->getHighestRow();
	$highestColumnIndex = $worksheet->getHighestColumn();
	
	for ($row = 1; $row <= $highestRowIndex; $row++) { 
	  
	  $headings = $worksheet->rangeToArray('A1:' . $highestColumnIndex . 1,null, true, false);
		array_walk($headings[0], function(&$value)
		{
		 // $value = str_replace(" ", "" ,strtolower($value));
		  $value = preg_replace("/[^a-zA-Z]+/", "",strtolower($value)); 
		});
			
			//	echo "<pre>";
         // print_r($headings);
          // echo "</pre>";
		
		
		$rowData = $worksheet->rangeToArray('A' . $row . ':' . $highestColumnIndex . $row, null, true, false);
		
 //print_r($rowData);
 
		$fields = array(
		  'title'
		, 'surname'
		, 'first_name'
		, 'middle_name'
		, 'gender'
		, 'dob'
		, 'marital_status'
		, 'occupation'
		, 'phone_number'
		, 'company_name'
		, 'registration_type'
		, 'registered_by'
		, 'tax_id'
		, 'office_address'
		, 'office_city'
		, 'temp_reg'
		, 'workplace_category'
		, 'active'
		, 'monthly_gross'
		, 'NHF'
		, 'NHIS'
		, 'NSITF'
		, 'basic_salary'
		, 'grade'
		, 'designation'
		, 'pension'
		, 'gratuity'
		); 
				
		$matches = array_intersect($headings[0],$fields);	

		$values = array();
		foreach($fields as $k => $v):
			if($v == "title"){
				$values[] = $title;
			}elseif($v == "surname"){
				$values[] = $surname;
			}elseif($v == "first_name"){
				$values[] = $firstname;
			}
			elseif($v == "middle_name"){
				$values[] = $middle_name;
			}
			elseif($v == "gender"){
				$values[] = $gender;
			}             
			elseif($v == "name's"){
				$values[] = $surname;
			}
			elseif($v == "fullname"){
				$values[] = $surname;
			}
			elseif($v == "dob"){
				$values[] = $dob;
			}
			elseif($v == "marital_status"){
				$values[] = $marital_status;
			}
			elseif($v == "occupation"){
				$values[] = $occupation2;
			}
			elseif($v == "phone_number"){
				$values[] = $phone_number;
			}
			elseif($v == "company_name"){
				$values[] = $institution;
			}
			elseif($v == "registration_type"){
				$values[] = '';
			}
			elseif($v == "registered_by"){
				$values[] = $UserLogin;
			}
			elseif($v == "tax_id"){
				$values[] = '';
			}
			elseif($v == "office_address"){
				$values[] = '';
			}
			elseif($v == "office_city"){
				$values[] = '';
			}
			elseif($v == "temp_reg"){
				$values[] = '';
			}
			elseif($v == "workplace_category"){
				$values[] = '';
			}
			elseif($v == "active"){
				$values[] = '';
			}
			elseif($v == "monthly_gross"){
				$values[] = $monthly_gross;
			}
			elseif($v == "NHF"){
				$values[] = $nhf;
			}
			elseif($v == "NHIS"){
				$values[] = $nhis;
			}elseif($v == "NSITF"){
				$values[] = $nsitf;
			}elseif($v == "basic_salary"){
				$values[] = $basic_salary;
			}elseif($v == "grade"){
				$values[] = $grade;
			}elseif($v == "designation"){
				$values[] = $designation;
			}elseif($v == "pension"){
				$values[] = $pension;
			}elseif($v == "gratuity"){
				$values[] = $gratuity;
			}
			else{
				$values[] = (in_array($v,$matches) ? $rowData[0][array_search($v,$matches)] : '');
			}		
		endforeach;
		
		$row_values = implode(',',$values);

and the result look like this:

Array
(
    [0] => 0
    [1] => 
    [2] => 0
    [3] => 0
    [4] => 0
    [5] => 
    [6] => 0
    [7] => ACCOUNTS CLERK
    [8] => 0
    [9] => DIAMOND BANK PLC (ABAKALIKI, OGOJA RD)
    [10] => 
    [11] => ezejonah
    [12] => 
    [13] => 
    [14] => 
    [15] => 
    [16] => 
    [17] => 
    [18] => 
    [19] => 0
    [20] => 0
    [21] => 0
    [22] => 0
    [23] => 
    [24] => 0
    [25] => 0
    [26] => 0
)

You can add this after your FOR loop for a quick check.


	echo "<pre>";
	print_r($matches);	
	print_r($headings);	
	echo "</pre>";

Why are all those variables defined with POST?
I thought the point was to read from file.

print_r($heading)=
Array
(
    [0] => Array
        (
            [0] => 
            [1] => names
            [2] => grade
            [3] => level
            [4] => state
            [5] => monthlygross
            [6] => conslugross
        )

)
print_r($matches)==
Array
(
    [2] => grade
)

Array
(
    [2] => grade
)

Array
(
    [2] => grade
)

Array
(
    [2] => grade
)

Array
(
    [2] => grade
)

Array
(
    [2] => grade
)

Array
(
    [2] => grade
)

Array
(
    [2] => grade
)

Array
(
    [2] => grade
)

Array
(
    [2] => grade
)

Array
(
    [2] => grade
)

Array
(
    [2] => grade
)

Array
(
    [2] => grade
)

Array
(
    [2] => grade
)

Array
(
    [2] => grade
)

Array
(
    [2] => grade
)

Array
(
    [2] => grade
)

Array
(
    [2] => grade
)

Array
(
    [2] => grade
)

Array
(
    [2] => grade
)

Array
(
    [2] => grade
)

Array
(
    [2] => grade
)

Well “grade” might match.

I also see your fields array still has UNDERSCORES… remove them.

WHAT DO THESE OTHER headings have to do with the database fields you are trying to populate?
Sure monthlygross will match as well after you remove the underscore from the fields array but what about “names”?

those variable is what holds values

$surname=$_POST['surname'];
	$firstname=$_POST['firstname'];
	$middlename=$_POST['middle_name'];
	$gender=$_POST['gender'];
	$dob=$_POST['dob'];
	$maritalstatus=$_POST['marital_status'];
	$occupation=$_POST['occupation'];
	$occupation2=$_POST['occupation2'];
	$phonenumber=$_POST['phone_number'];
	$monthly_gross=$_POST['monthly_gross'];
	$nhf=$_POST['nhf'];
	$nhis=$_POST['nhis'];
	$nsitf=$_POST['nsitf'];
	$basic_salary=$_POST['basic_salary'];
	$grade=$_POST['grade'];
	$designation=$_POST['designation'];
	$pension=$_POST['pension'];
	$gratuity=$_POST['gratuity'];






if($v == "title"){
				$values[] = $title;
			}elseif($v == "surname"){
				$values[] = $surname;
			}elseif($v == "first_name"){
				$values[] = $firstname;
			}
			elseif($v == "middle_name"){
				$values[] = $middlename;
			}
			elseif($v == "gender"){
				$values[] = $gender;
			}             
			elseif($v == "name's"){
				$values[] = $surname;
			}
			elseif($v == "fullname"){
				$values[] = $surname;
			}
			elseif($v == "dob"){
				$values[] = $dob;
			}
			elseif($v == "marital_status"){
				$values[] = $maritalstatus;
			}
			elseif($v == "occupation"){
				$values[] = $occupation2;
			}
			elseif($v == "phone_number"){
				$values[] = $phone_number;
			}
			elseif($v == "company_name"){
				$values[] = $institution;
			}
			elseif($v == "registration_type"){
				$values[] = '';
			}
			elseif($v == "registered_by"){
				$values[] = $UserLogin;
			}
			elseif($v == "tax_id"){
				$values[] = '';
			}
			elseif($v == "office_address"){
				$values[] = '';
			}
			elseif($v == "office_city"){
				$values[] = '';
			}
			elseif($v == "temp_reg"){
				$values[] = '';
			}
			elseif($v == "workplace_category"){
				$values[] = '';
			}
			elseif($v == "active"){
				$values[] = '';
			}
			elseif($v == "monthly_gross"){
				$values[] = $monthlygross;
			}
			elseif($v == "NHF"){
				$values[] = $nhf;
			}
			elseif($v == "NHIS"){
				$values[] = $nhis;
			}elseif($v == "NSITF"){
				$values[] = $nsitf;
			}elseif($v == "basic_salary"){
				$values[] = $basicsalary;
			}elseif($v == "grade"){
				$values[] = $grade;
			}elseif($v == "designation"){
				$values[] = $designation;
			}elseif($v == "pension"){
				$values[] = $pension;
			}elseif($v == "gratuity"){
				$values[] = $gratuity;
			}

Why are you still using POST?

names ,fullname or other come in when the file formate have all name in one column.
that is why i added it

for the case of post… bros. am seriously confuse here dont know what to do again. pls guide me.
so that i will not be lost.

Please remove underscores and apostrophes from ALL names used in fields array.
IF you have added a DB field called “name” Update the fields array for this field and the INSERT QUERY making sure you keep them both in the same order.
Go back to using the original foreach statement which only had the few hard coded values.

		foreach($fields as $k => $v):
			if($v == "occupation"){
				$values[] = $textfile_occupation;
			}elseif($v == "companyname"){
				$values[] = $institution;
			}elseif($v == "registeredby"){
				$values[] = $UserLogin;
			}else{
				$values[] = (in_array($v,$matches) ? $rowData[0][array_search($v,$matches)] : '');
			}		
		endforeach;

Once again print out headings and matches arrays.

for matches
Array
(
[2] => grade
)

Array
(
[2] => grade
)

Array
(
[2] => grade
)

Array
(
[2] => grade
)

Array
(
[2] => grade
)

heading

Array
(
[0] => Array
(
[0] =>
[1] => names
[2] => grade
[3] => level
[4] => state
[5] => monthlygross
[6] => conslugross
)

)

if($v == "occupation"){
				$values[] = $textfileoccupation;
			}elseif($v == "companyname"){
				$values[] = $institution;
			}elseif($v == "registeredby"){
				$values[] = $UserLogin;
			}
			else{
				$values[] = (in_array($v,$matches) ? $rowData[0][array_search($v,$matches)] : '');
			}

did you remove underscore from all names used in fields array?
monthlygross should have matches.

Did you add names to fields array???
Did you add DB field name to your table???

How are you planning on getting other missing fields?
The xls headings are nowhere close to matching the fields array?

Array
(
[2] => grade
[5] => monthlygross
)
yes i have remove all the under_score

i have surname,firstname,lastname in my table.
in case of missing field, i dont know how am going to handle that. or do have have in mind what i should do?

OK so as you can see, if the fields array name matches on in the headings array the matches array will hold that field name so you can search for the value.