kamshory / ZKLibrary

ZKLibrary is PHP library for reading and writing data to attendance device using UDP protocol. This library useful to comunicate between web server and attendance device directly without addition program. This library is implemented in the form of class. So that you can create an object and use it functions.
GNU General Public License v2.0
193 stars 176 forks source link

save attendance in database #121

Open Aminechouchene98 opened 1 year ago

Aminechouchene98 commented 1 year ago

i am trying to save attendance in database but i get this error can someone help me err1 err2 ?

erkutcin commented 1 year ago

i am trying to save attendance in database but i get this error can someone help me err1 err2 ?

Hi Aminechouchene98, i made all updates. But i am still not able to retrieve the datas. Could you advise me how did you do it? Thanks in advance

IncubuzzCC commented 1 year ago

Hi erkutcin , This likely seems to be a database issue on your end, and not a device issue with the library.

In my case the uid returns the position the user record is stored in the device while id is the actual user id. In your case it looks like yours is storing a consecutive id in the uid field and the user id in the id field. Therefore thw data structure may slightly vary across devices.

As long as you do not clear attendance records the device by using the clearAttendance() function, the device will give you the same data plus the new attendance records. If you clear the attendance records, which you probably should every once in a while to keep the download from becoming slow,

We worked around this by creating a combined primary key (or you could use a unique index) over the user id and the date time field.

CREATE TABLE ``deviceusers` (
  `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(75) COLLATE utf8mb4_unicode_ci NOT NULL,
  `synctime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `fetched` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
);

CREATE TABLE `deviceregs` (
  `deviceuser_id` smallint(5) unsigned NOT NULL,
  `time` datetime NOT NULL,
  `fetched` tinyint(3) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`deviceuser_id`,`time`),
  CONSTRAINT `deviceregs_deviceuser_id_foreign` FOREIGN KEY (`deviceuser_id`) REFERENCES `deviceusers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
);

This way you could use INSERT IGNORE or capture inserts that fail due to duplication.

I hope this helps.

Sample device attendance:

[23] => Array([0] => 29, [1] => 3, [2] => 0, [3] => 2022-12-09 19:02:00)
[24] => Array([0] => 9, [1] => 9, [2] => 0, [3] => 2022-12-10 07:39:07)
[25] => Array([0] => 163, [1] => 162, [2] => 0, [3] => 2022-12-10 07:41:35)

Sample device users:

[9] => Array([0] => 9, [1] => Josef, [2] => 0, [3] => )
[29] => Array([0] => 3, [1] => Fred, [2] => 14, [3] => 696910)
[163] => Array([0] => 162, [1] => Bernard, [2] => 0, [3] => )