Add form data to database : Form « CGI « Perl






Add form data to database

 
<html>
<head>
<title>Add New Contact</title>
</head>
<body>
<div align=center>
Contact Database<br><br>
Add New Contact<br><br>
</div>

<form name="addrec" method="post" action="insertrec.pl"><br>
    Firstname<input name="firstname" type="text">
    Middle Initial<input name="middle" type="text" size="3">
    Last Name<input name="lastname" type="text" size="20">
    Address<input name="street" type="text" size="40">
    Town<input name="town" type="text" size="15">
    Zip<input name="postcode" type="text" size="15">
    Phone<input name="phone" type="text" size="10">
    e-mail<input name="email" type="text" size="20">
    Comments<textarea name=comments rows=4 cols=50></textarea>
    <input type="button" name="addit" value="Add Contact"></td>
    <td><input type="reset" name="reset" value="Clear Form"></td>
</body>

</html>

//File: insertrec.pl
#!c:/perl/bin

use Win32::OLE;
use Win32::OLE::Const 'Microsoft ActiveX Data Objects';
use CGI ':standard';

print header();

$firstname = param("firstname");
$lastname  = param("lastname");
$middle    = param("middle");
$street    = param("street");
$town      = param("town");
$postcode  = param("postcode");
$phone     = param("phone");
$email     = param("email");
$comments  = param("comments");

$table  = "contact_table";

$conn = Win32::OLE->new("ADODB.Connection");
$rs   = Win32::OLE->new("ADODB.Recordset");

$sql = "INSERT INTO $table (firstname, middle, lastname, street, town,postcode, telephone, email, comments)
        VALUES ('$firstname', '$middle', '$lastname', '$street', '$town','$postcode', '$phone', '$email', '$comments')";

$conn->Open("contact");
$conn->Execute($sql);

print "<br>Contacts Database<br>";
print "Record Added!<br>";
print "<a href=/addcontact.htm>Add Another Contact?</a><br><br>";
print "<a href=/home.htm>Home</a><br><br>";

$rs->Close;
$conn->Close;

   
  








Related examples in the same category

1.Form Input Types
2.Demonstrates GET method with HTML form.
3.Demostrates POST method with HTML form.
4.Demonstrates use of CGI.pm with HTML form.
5.Create HTML form with CGI
6.Get form submitted value
7.Using param() function to get parameter
8.Using CGI function to check the parameter
9.Create a form and set the method and action
10.Create a form with submit button
11.Process form with regular expression: first name and last name
12.Process form with regular expression: date
13.Process form with regular expression: time
14.Generate the HTML form
15.Printing the Name Input Using the CGI Module
16.Get form value with param
17.Generate and Process Forms
18.Form Mail
19.Form based table editing
20.Create a form with Perl code
21.Code to Accept Input with the CGI Module
22.Capitalize the first letter of each parameter using ucfirst
23.A Form-Based Example
24.Learn about the current CGI request
25.Read the data for a CGI GET request
26.Read the data passed to a script on the command line?
27.Decoding the Input Data
28.The POST Method
29.Verifying a username and a password
30.Sample Database Query
31.Writing a cookie to the client computer
32.Add a New Phone Number
33.Sessions - Preserving State
34.Separate the form and perl script
35.Passing parameter to perl CGI code
36.EMail sending form
37.Guest book form
38.Data-Entry Forms in Web Pages
39.Using LI
40.Using the option select box
41.Reading text in textarea
42.Querying all the parameters