Following is the array from which the data are retrieved and tried to be inserted into mysql table but works well when more than 1 row to be inserted and fails when only 1 row to be inserted.
// The structure of the array is as follows
Array
(
[Row] => Array
(
[0] => Array
(
[PIN] => 1274
[DateTime] => 2018-04-07 09:28:16
[Verified] => 15
[Status] => 3
[WorkCode] => 0
)
[1] => Array
(
[PIN] => 157
[DateTime] => 2018-04-07 10:22:56
[Verified] => 15
[Status] => 3
[WorkCode] => 0
)
// these are the raw punch data from biometric machine
Following is the piece of code which currently being used.
if(isset($array_att_logs) && isset($array_att_logs['Row'])) {
foreach ($array_att_logs['Row'] as $value) {
$emp_code = $value['PIN']; // line 92 $dateNTime = $value['DateTime']; //line 93
$punch_query = "INSERT IGNORE INTO punching_data_table (emp_code, date_time,in_out_status) VALUES ('$emp_code', '$dateNTime','$in_out_status')";
$punch_result = mysql_query($punch_query);
echo mysql_error();
}
} echo var_dump($array_att_logs);
Please note that the $in_out_status is defined explicitly elsewhere which does not seem to have any problem at all.I have checked using var_dump()
that the array exists with data just fine. The problem is if there is a single row to be inserted into the table then following error is shown.
Warning: Illegal string offset 'PIN' in /home1/.../punch-raw.php on line 92 Warning: Illegal string offset 'DateTime' in /home1/..../punch-raw.php on line 93
If there is more than 1 row to be inserted into the table then it works just fine. May be I am defining $emp_code
and $dateNTime
incorrectly.
Here is how it looks like when there is a single row.
echo " <pre>";
print_r($array_att_logs);
echo "/<pre>";
Array
(
[Row] => Array
(
[PIN] => 406
[DateTime] => 2018-05-06 14:40:09
[Verified] => 1
[Status] => 3
[WorkCode] => 0
)
)
/
using var_dump it looks like below
array(1) { ["Row"]=> array(5) { ["PIN"]=> string(3) "406" ["DateTime"]=> string(19) "2018-05-06 14:40:09" ["Verified"]=> string(1) "1" ["Status"]=> string(1) "3" ["WorkCode"]=> string(1) "0" } }
</div>