Jump to content

How can i insert data from biomatric device to mysql + PHP


Go to solution Solved by mac_gyver,

Recommended Posts

I'm Trying to INSERT Data into mysql which is generated from biomatric attendance device.
Currently when the page loads all the data in the device will be displaying in a table.

what i wanted to do is to save these data into mysql database.
i have tried few codes none of these code works for me .
please let me know how can i do this .

<?php include 'db.php'; ?>
<!DOCTYPE html>
<html>
  <head>
      <meta charset="utf-8">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
	  <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
    <!-- <script type="text/javascript" src="load.js"></script> -->
    <link rel="stylesheet" href="style.css">
    <title>Live Attendance</title>
  </head>
    <?php
include("zklib/zklib.php");
$zk = new ZKLib("192.168.1.3", 4370);
$ret = $zk->connect();
sleep(1);
if ( $ret ):
    $zk->disableDevice();
    sleep(1);
     ?>

    <body>


    <table border="1" cellpadding="5" cellspacing="2" style="float: left; margin-right: 10px;">
        <tr>
            <th colspan="5">Data User</th>
        </tr>
        <tr>
            <th>UID</th>
            <th>ID</th>
            <th>Name</th>
            <th>Role</th>
            <th>Password</th>
        </tr>
        <?php
        try {

            //$zk->setUser(1, '1', 'Admin', '', LEVEL_ADMIN);
            $user = $zk->getUser();
            sleep(1);
            while(list($uid, $userdata) = each($user)):
                if ($userdata[2] == LEVEL_ADMIN)
                    $role = 'ADMIN';
                elseif ($userdata[2] == LEVEL_USER)
                    $role = 'USER';
                else
                    $role = 'Unknown';
            ?>
            <tr>
                <td><?php echo $uid ?></td>
                <td><?php echo $userdata[0] ?></td>
                <td><?php echo $userdata[1] ?></td>
                <td><?php echo $role ?></td>
                <td><?php echo $userdata[3] ?> </td>
            </tr>
            <?php
            endwhile;
        } catch (Exception $e) {
            header("HTTP/1.0 404 Not Found");
            header('HTTP', true, 500); // 500 internal server error
        }
        //$zk->clearAdmin();

      
        if (isset($_POST['create_post'])) {

          $query = "INSERT INTO attendance(idx)";
          $query .= "VALUES ($id)";
          $submittodbquery = mysqli_query($con,$query);
          if (!$submittodbquery) {
          die("Failed up upload " . mysqli_error($con));
        }else {
          echo "updated";
        }

        }



        ?>
    </table>

    <button id="submit" type="submit" name="create_post" class="btn btn-danger">Submit New Post</button>


        <?php
        $name = $userdata[1];
        $attendance = $zk->getAttendance();
        sleep(1);
        while(list($idx, $attendancedata ) = each($attendance)):
            if ( $attendancedata[2] == 1 )
                $status = 'Check Out';
            else
                $status = 'Check In';



        ?>

            <?php  $idx ?>
            <?php  $attendancedata[0] ?>
            <?php  $attendancedata[1] ?>
            <?php  $status ?>
            <?php  date( "d-m-Y", strtotime( $attendancedata[3] ) ) ?>
            <?php  date( "H:i:s", strtotime( $attendancedata[3] ) ) ?>

            <?php
            // custom code


            if (isset($_POST['submit'])) {

              $query = "INSERT INTO attendance(idx)";
              $query .= "VALUES ($idx)";
              $submittodbquery = mysqli_query($con,$query);
              if (!$submittodbquery) {
              die("Failed up upload " . mysqli_error($con));
            }else {
              echo "updated";
            }

            }
             ?>



                         <?php
                         endwhile
                         ?>
                     </table>
                     <input type="submit" name="submit" value="Update Database">

                 <?php
                     $zk->enableDevice();
                     sleep(1);
                     $zk->disconnect();
                 endif
             ?>






                 </body>
             </html>

Thanks for reading my question ..

post-202426-0-56457500-1479970693_thumb.png

  • Solution

it would be helpful if you define what the work-flow (steps) is (are) before you try to write code. it will also help us if you tell us what the work-flow is supposed to be. something like - 1) connect to device, 2) retrieve user and attendance data, 3) display data in forms, 4) submit form, 5) store submitted data into database table.

 

are you displaying the data as part of the learning/debugging process and you want to 'automatically' insert the data into the database tables OR are you trying to display the data in a form and you will manually submit the form to cause the data to be inserted into the database tables?

 

next, you need to separate the different concerns in your code.

 

any form processing code should be near the top of your file and come before you output any html to the page. the form processing code would come before the <!DOCTYPE tag. all the form processing code would be grouped together.

 

to display the page, your code is retrieving user and attendance data from the time-clock. the code to retrieve this data and store it in php variables should be grouped together and come before you start outputting the html document. you would then simply loop over this data when you output the html document.

 

if you are outputting the data in a form, perhaps to allow it to be verified and edited by a manager, you would need to output it using form fields, in a valid form. there are currently no <form...> tag(s) and no data-input form fields in your code. your attendance data output doesn't even have a <table> tag, no <tr></tr> <td></td> tags and no echo statements for the data.

Here is current steps i'm able to do following steps with the current code ..

1- connecting the device .

2 - pull the current data from attendance device .

3 dispay the data in current page (in this case index.php)

  pulled information

   1- user id (from attendance device)

   2- username

   3- index number

   4- check in status

   5-date

   6- time

 

i'm trying to manully insert the data (via button)  into mysql database . if user pressed the button

which currently unable to process

 

i've removed form fields and tables because i don't want to display data in current page.

  • 2 years later...
  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.