Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- use DBI;
- $db="users";
- $user="root";
- $passwd="";
- $host="localhost";
- $connectionInfo="dbi:mysql:$db;$host";
- print "Content-Type:text/html\n\n";
- if ($ENV {REQUEST_METHOD} eq "GET") {
- &displayform;
- exit;
- }
- else
- {
- &parseform();
- &insertuser();
- exit;
- }
- sub parseform
- {
- read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
- @pairs = split(/&/, $buffer);
- foreach $pair (@pairs) {
- ($name, $value) = split(/=/, $pair);
- $value =~ tr/+/ /;
- $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
- $form{$name} = $value;
- }
- }
- sub insertuser
- {
- $insert = qq~insert accounts (userid, firstname, lastname, emailaddress)
- values ('$form{userid}', '$form{firstname}','$form{lastname}','$form{emailaddress}')~;
- $dbh=DBI->connect($connectionInfo,$user,$passwd);
- $sth=$dbh-> prepare($insert);
- if ($sth->execute())
- {
- print "<h2>Success!</h2>";
- print "<a href='http://172.16.2.6/cgi-bin/ListAccounts.cgi'>Click here to list all user accounts</a>";
- }
- else {
- print "<h2>Failure</h2>";
- }
- $dbh->disconnect();
- }
- sub displayform
- {
- print <<HTMLCODE;
- <html>
- <head>
- <title>Add Account</title>
- </head>
- <body>
- <form action="AddAccounts.cgi" method=post>
- <center>
- <h2>Add a user account</h2>
- Username: <input type=text name=userid value="$form{userid}">
- <br>
- First Name: <input type=text name=firstname value="$form{firstname}">
- <br>
- Last Name: <input type=text name=lastname value="$form{lastname}">
- <br>
- Email Address: <input type=text name=emailaddress value="$form{emailaddress}">
- <br>
- <input type=submit value="Insert" name=Insert>
- </form>
- </body>
- </html>
- HTMLCODE
- }
- ----=-=-=-=--=-ListAccounts=-=-=-
- #!/usr/bin/perl -w
- use DBI;
- $db="users";
- $user="root";
- $passwd="";
- $host="localhost";
- $connectionInfo="dbi:mysql:$db;$host";
- print "Content-Type:text/html\n\n";
- &listaccounts();
- sub listaccounts
- {
- print qq~
- <html>
- <head>
- <title>User Accounts</title>
- </head>
- <body>
- <table border=1>
- <tr>
- <th>Username</th><th>First Name</th><th>Last Name</th><th>Email Address</th>
- </tr>~;
- $select = qq~select userid, firstname,lastname, emailaddress from accounts~;
- $dbh=DBI->connect($connectionInfo, $user, $passwd);
- $sth=$dbh->prepare($select);
- #execute select statement
- $sth->execute();
- #Loop through each record selected and print in html table
- while (@row=$sth->fetchrow_array())
- {
- print qq~
- <tr>
- <td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td>
- </tr>~;
- }
- #Close HTML table
- print qq~
- </table>
- </body>
- </html>
- ~;
- $dbh->disconnect();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement