<html>
<head>
<title></title>
</head>
<body>

<h2>Update Mac Address Registration</h2>

<?php
  /************************************************************/
  // Modify the MAC address registration in the management database
  // the database connection parameters might be modified.
  // As this script should be used only by the administrators, 
  // it should be protected by some access control method.
  /************************************************************/
   
  // if userid is not set, request input and return
  if(!isset($_POST['userId'])){
    print('<form method=POST action=updatemactable.php>');
    print('UserId:<input type=text name=userId>');
    print('<input type=submit value="Send">');
    print('<input type=reset value="Reset">');
    print('</form>');
    return;
  }
  $userId=$_POST['userId'];
  
  // setup MySql DB
  $link = mysqli_connect('localhost', 'root', '');
  if (!$link) die('Cannot connect DB'.mysqli_error());

  $db_selected = mysqli_select_db($link, 'opengatem');
  if (!$db_selected) die('cannot select DB'.mysqli_error());

  mysqli_set_charset($link, 'utf8');

  // if data is posted, update DB
  $i=0;
  while(isset($_POST['macAddress'][$i])){

    if($_POST['status'][$i]=='D'){
      // on delete 
      // UPDATE macaddrs SET status="D",device="postdata",limitdate=now(),mailAddress="postdata"
      //       WHERE macAddress="postdata" and userId="postdata" and status!="D"

      $result = mysqli_query($link, 'UPDATE macaddrs SET status="'.$_POST['status'][$i]
              .'", device="'.$_POST['device'][$i].'", limitDate=now()'
              .', mailAddress="'.$_POST['mailAddress'][$i].'" WHERE macAddress="'
              .$_POST['macAddress'][$i].'" and userId="'.$userId.'" and status!="D"');
      if (!$result) die('Fail update query'.mysqli_error());

    }else if($_POST['status'][$i]=='A'||$_POST['status'][$i]=='I'){
      // other cases
      // UPDATE macaddrs SET status="D",device="postdata",limitdate="postdata",mailAddress="postdata"
      //       WHERE macAddress="postdata" and userId="postdata" and status!="D"

      $result = mysqli_query($link, 'UPDATE macaddrs SET status="'.$_POST['status'][$i]
              .'", device="'.$_POST['device'][$i].'", limitDate="'.$_POST['limitDate'][$i]
              .'", mailAddress="'.$_POST['mailAddress'][$i].'" WHERE macAddress="'
              .$_POST['macAddress'][$i].'" and userId="'.$userId.'" and status!="D"');
      if (!$result) die('Fail update query'.mysqli_error());

    }else{
      print("<font color=red>Illegal status value</font>");
    }
    $i++;
  }

  // get data from DB to show on web
  $result = mysqli_query($link, 'SELECT macAddress,status,device,limitDate,mailAddress FROM macaddrs where userId="'
            .$userId.'"');
  if (!$result) die('Fail select query'.mysqli_error());

 // print html form
  print('<form method=POST action=updatemac.php>');
  print('UserId:<input type=text name=userId value="'.$userId.'">');
  print('<input type=submit value="Send">');
  print('<input type=reset value="Reset">');

  // print header
  print("<table border=1>");
  print('<tr>');
  $count=0;
  while ($field = mysqli_fetch_field($result)) {
    print('<td>'.$field->name.'</td>');
    $fieldName[$count]=$field->name;
    $count++;
  }
  print('</tr>');

  // print rows
  while ($row = mysqli_fetch_row($result)) {
    print('<tr>');
    if($row[1]=='D'){  // deleted items
      for($i=0; $i<$count; $i++){
        print('<td>'.$row[$i].'</td>');
      }
    }else{                   // other items
      for($i=0; $i<$count; $i++){
        if($i==0){
          print('<td><input size=30 type=text name='.$fieldName[$i].'[] value="'.$row[$i].'" readonly></td>');
        }else{
          print('<td><input size=30 type=text name='.$fieldName[$i].'[] value="'.$row[$i].'"></td>');
        }
      }
    }
    print('</tr>');
  }
  print('</table>');
  print('</form>');
  $close_flag = mysqli_close($link);

?>
<p>status char: A=Actice, I=InActive, D=Deleted</p>
</body>
</html>
