Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use DBI;
  4.  
  5. $db="users";
  6. $user="root";
  7. $passwd="";
  8. $host="localhost";
  9. $connectionInfo="dbi:mysql:$db;$host";
  10.  
  11. print "Content-Type:text/html\n\n";
  12.  
  13. if ($ENV {REQUEST_METHOD} eq "GET") {
  14. &displayform;
  15. exit;
  16. }
  17.  
  18. else
  19. {
  20. &parseform();
  21. &insertuser();
  22. exit;
  23. }
  24.  
  25. sub parseform
  26. {
  27. read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  28. @pairs = split(/&/, $buffer);
  29. foreach $pair (@pairs) {
  30. ($name, $value) = split(/=/, $pair);
  31. $value =~ tr/+/ /;
  32. $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  33. $form{$name} = $value;
  34. }
  35. }
  36.  
  37. sub insertuser
  38. {
  39. $insert = qq~insert accounts (userid, firstname, lastname, emailaddress)
  40. values ('$form{userid}', '$form{firstname}','$form{lastname}','$form{emailaddress}')~;
  41. $dbh=DBI->connect($connectionInfo,$user,$passwd);
  42. $sth=$dbh-> prepare($insert);
  43. if ($sth->execute())
  44. {
  45. print "<h2>Success!</h2>";
  46. print "<a href='http://172.16.2.6/cgi-bin/ListAccounts.cgi'>Click here to list all user accounts</a>";
  47. }
  48. else {
  49. print "<h2>Failure</h2>";
  50. }
  51. $dbh->disconnect();
  52. }
  53. sub displayform
  54. {
  55. print <<HTMLCODE;
  56. <html>
  57. <head>
  58. <title>Add Account</title>
  59. </head>
  60. <body>
  61. <form action="AddAccounts.cgi" method=post>
  62. <center>
  63. <h2>Add a user account</h2>
  64. Username: <input type=text name=userid value="$form{userid}">
  65. <br>
  66. First Name: <input type=text name=firstname value="$form{firstname}">
  67. <br>
  68. Last Name: <input type=text name=lastname value="$form{lastname}">
  69. <br>
  70. Email Address: <input type=text name=emailaddress value="$form{emailaddress}">
  71. <br>
  72. <input type=submit value="Insert" name=Insert>
  73. </form>
  74. </body>
  75. </html>
  76. HTMLCODE
  77. }
  78.  
  79.  
  80. ----=-=-=-=--=-ListAccounts=-=-=-
  81. #!/usr/bin/perl -w
  82.  
  83. use DBI;
  84.  
  85. $db="users";
  86. $user="root";
  87. $passwd="";
  88. $host="localhost";
  89. $connectionInfo="dbi:mysql:$db;$host";
  90.  
  91. print "Content-Type:text/html\n\n";
  92.  
  93. &listaccounts();
  94.  
  95. sub listaccounts
  96. {
  97. print qq~
  98. <html>
  99. <head>
  100. <title>User Accounts</title>
  101. </head>
  102. <body>
  103. <table border=1>
  104. <tr>
  105. <th>Username</th><th>First Name</th><th>Last Name</th><th>Email Address</th>
  106. </tr>~;
  107. $select = qq~select userid, firstname,lastname, emailaddress from accounts~;
  108. $dbh=DBI->connect($connectionInfo, $user, $passwd);
  109. $sth=$dbh->prepare($select);
  110.  
  111. #execute select statement
  112. $sth->execute();
  113.  
  114. #Loop through each record selected and print in html table
  115. while (@row=$sth->fetchrow_array())
  116. {
  117. print qq~
  118. <tr>
  119. <td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td>
  120. </tr>~;
  121. }
  122.  
  123. #Close HTML table
  124. print qq~
  125. </table>
  126. </body>
  127. </html>
  128. ~;
  129.  
  130. $dbh->disconnect();
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement