Advertisement
Guest User

fr2-mysql.cgi

a guest
Feb 19th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. #USe the DBI(db interface) module
  4. use DBI;
  5.  
  6. #Declare variables with MySQL connection data
  7. $db="dbname/user";
  8. $user="zenuser";
  9.  
  10. $passwd="pass";
  11. $host="db-mysql.zen";
  12. $connectionInfo="dbi:mysql:$db;$host";
  13.  
  14. #Print HTTP Headers
  15. print "Content-type:text/html\n\n";
  16.  
  17. #If first time script run display form
  18. if ($ENV{REQUEST_METHOD} eq "GET")
  19. {
  20. &displayform();
  21. &showfriends();
  22. exit;
  23. }
  24. #else process form and insert into DB
  25. else
  26. {
  27. &parseform();
  28. &insertfriend();
  29. &showfriends();
  30. exit;
  31. }
  32. #Standard form parseing using POST method
  33. sub parseform
  34. {
  35. # Read the standard input (sent by the form):
  36. read(STDIN, $FormData, $ENV{'CONTENT_LENGTH'});
  37. # Get the name and value for each form input:
  38. @pairs = split(/&/, $FormData);
  39. # Then for each name/value pair....
  40. foreach $pair (@pairs) {
  41. # Separate the name and value:
  42. ($name, $value) = split(/=/, $pair);
  43. # Convert + signs to spaces:
  44. $value =~ tr/+/ /;
  45. # Convert hex pairs (%HH) to ASCII characters:
  46. $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  47. # Store values in a hash called %FORM:
  48. $form{$name} = $value;
  49. }
  50.  
  51.  
  52. }
  53.  
  54. sub insertfriend
  55. {
  56.  
  57. #Form SQL insert statement
  58. $insert = qq~insert friends(lname, fname, phone, email)
  59. values('$form{lname}', '$form{fname}', '$form{phone}', '$form{email}')~;
  60.  
  61. #connect to MYSQL and create Database Handler $dbh
  62. $dbh=DBI->connect($connectionInfo,$user,$passwd);
  63.  
  64. #Prepare MYSQL Statement and create Statement Handler $sth
  65. $sth=$dbh->prepare($insert);
  66.  
  67. #Execute Statement Handler and test for success
  68. if ($sth->execute())
  69. {
  70. &displaysuccess;
  71. #&showfriends;
  72. }
  73. else
  74. {
  75. &displayfail;
  76. }
  77.  
  78. #Disconnect Database
  79. $dbh->disconnect();
  80. }
  81. sub displaysuccess
  82. {
  83. print qq~<html>\n
  84. <head>
  85. <title>My Friends</title>
  86. </head>
  87. <body>
  88. <h2>Record Added</h2>
  89. </body>
  90. </html>
  91. ~;
  92. }
  93. sub displayfail
  94. {
  95. print qq~<html>\n
  96. <head>
  97. <title>My Friends</title>
  98. </head>
  99. <body>
  100. <h2>Record NOT Added</h2>
  101. </body>
  102. </html>
  103. ~;
  104. }
  105. sub displayform
  106. {
  107. print qq~
  108. <html>
  109. <head>
  110. <title>My Friends</title>
  111. </head>
  112.  
  113. <body>
  114. <form action="friends2-mysql.cgi" method=POST>
  115. <center>
  116. <h2>My Friends</h2>
  117. Last Name: <input type=text name=lname>
  118. <br>
  119. First Name: <input type=text name=fname>
  120. <br>
  121. Phone Number: <input type=text name=phone> (10 digits only)
  122. <br>
  123. E-mail: <input type=text name=email>
  124. <br>
  125. <input type=submit value="Insert" name=Insert>
  126. <input type=reset value=Reset name=reset>
  127. </form>
  128. </body>
  129. </html>
  130. ~;
  131. }
  132.  
  133. sub showfriends
  134. {
  135. #start html table
  136. print qq~<html>
  137. <head>
  138. <Title>My Friends</Title>
  139. </head>
  140. <body>
  141. <table border=1>
  142. <tr>
  143. <th>ID</th><th>Last Name</th><th>First Name</th><th>Phone Number</th><th>Email</th>
  144. </tr>~;
  145.  
  146. #form SQL Select Statement
  147. $select = qq~select id, lname, fname, phone, email from friends~;
  148.  
  149. #connect to MySQL and create a DB handler $dbh
  150. $dbh=DBI->connect($connectionInfo,$user,$passwd);
  151.  
  152. #prepare MySQL statement and create Statement Handler $sth
  153. $sth=$dbh->prepare($select);
  154.  
  155. #Execute select statement
  156. $sth->execute();
  157.  
  158. #Loop through each record selected and print in html table
  159. while (@row=$sth->fetchrow_array())
  160. {
  161. print qq~<tr>
  162. <td>$row[0]</td><td>$row[1]</td><td>$row[2]</td>
  163. <td>$row[3]</td><td>$row[4]</td>
  164. </tr>~;
  165. }
  166.  
  167. #close HTML table
  168. print qq~</table>
  169. </body>
  170. </html>~;
  171.  
  172. $dbh->disconnect();
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement