OSDN Git Service

Ver.0.8.1
[opengatem/opengatem.git] / phpsrc / updatemactable.php
1 <html>
2 <head>
3 <title></title>
4 </head>
5 <body>
6
7 <h2>Update Mac Address Registration</h2>
8
9 <?php
10
11   // check basic auth
12   if(getenv('REMOTE_USER')=='')  die('Cannot get auth');
13
14   // if userid is not set, request input and return
15   if(!isset($_POST['userId'])){
16     print('<form method=POST action=updatemac.php>');
17     print('UserId:<input type=text name=userId>');
18     print('<input type=submit value="Send">');
19     print('<input type=reset value="Reset">');
20     print('</form>');
21     return;
22   }
23   $userId=$_POST['userId'];
24   
25   // setup MySql DB
26   $link = mysql_connect('localhost', 'root', '');
27   if (!$link) die('Cannot connect DB'.mysql_error());
28
29   $db_selected = mysql_select_db('opengatem', $link);
30   if (!$db_selected) die('cannot select DB'.mysql_error());
31
32   mysql_set_charset('utf8');
33
34   // if data is posted, update DB
35   $i=0;
36   while(isset($_POST['macAddress'][$i])){
37
38     if($_POST['status'][$i]=='D'){
39       // on delete 
40       // UPDATE macaddrs SET status="D",device="postdata",limitdate=now(),mailAddress="postdata"
41       //       WHERE macAddress="postdata" and userId="postdata" and status!="D"
42
43       $result = mysql_query('UPDATE macaddrs SET status="'.$_POST['status'][$i]
44               .'", device="'.$_POST['device'][$i].'", limitDate=now()'
45               .', mailAddress="'.$_POST['mailAddress'][$i].'" WHERE macAddress="'
46               .$_POST['macAddress'][$i].'" and userId="'.$userId.'" and status!="D"');
47       if (!$result) die('Fail update query'.mysql_error());
48
49     }else if($_POST['status'][$i]=='A'||$_POST['status'][$i]=='I'){
50       // other cases
51       // UPDATE macaddrs SET status="D",device="postdata",limitdate="postdata",mailAddress="postdata"
52       //       WHERE macAddress="postdata" and userId="postdata" and status!="D"
53
54       $result = mysql_query('UPDATE macaddrs SET status="'.$_POST['status'][$i]
55               .'", device="'.$_POST['device'][$i].'", limitDate="'.$_POST['limitDate'][$i]
56               .'", mailAddress="'.$_POST['mailAddress'][$i].'" WHERE macAddress="'
57               .$_POST['macAddress'][$i].'" and userId="'.$userId.'" and status!="D"');
58       if (!$result) die('Fail update query'.mysql_error());
59
60     }else{
61       print("<font color=red>Illegal status value</font>");
62     }
63     $i++;
64   }
65
66   // get data from DB to show on web
67   $result = mysql_query('SELECT macAddress,status,device,limitDate,mailAddress FROM macaddrs where userId="'
68             .$userId.'"');
69   if (!$result) die('Fail select query'.mysql_error());
70
71  // print html form
72   print('<form method=POST action=updatemac.php>');
73   print('UserId:<input type=text name=userId value="'.$userId.'">');
74   print('<input type=submit value="Send">');
75   print('<input type=reset value="Reset">');
76
77   // print header
78   print("<table border=1>");
79   print('<tr>');
80   $count=0;
81   while ($field = mysql_fetch_field($result)) {
82     print('<td>'.$field->name.'</td>');
83     $fieldName[$count]=$field->name;
84     $count++;
85   }
86   print('</tr>');
87
88   // print rows
89   while ($row = mysql_fetch_row($result)) {
90     print('<tr>');
91     if($row[1]=='D'){  // deleted items
92       for($i=0; $i<$count; $i++){
93         print('<td>'.$row[$i].'</td>');
94       }
95     }else{                   // other items
96       for($i=0; $i<$count; $i++){
97         if($i==0){
98           print('<td><input size=30 type=text name='.$fieldName[$i].'[] value="'.$row[$i].'" readonly></td>');
99         }else{
100           print('<td><input size=30 type=text name='.$fieldName[$i].'[] value="'.$row[$i].'"></td>');
101         }
102       }
103     }
104     print('</tr>');
105   }
106   print('</table>');
107   print('</form>');
108   $close_flag = mysql_close($link);
109
110 ?>
111 <p>status char: A=Actice, I=InActive, D=Deleted</p>
112 </body>
113 </html>