#!/usr/bin/perl -w # #example of putting data into a database use strict; use CGI; use DBI; use vars qw($dsn $username $password); #get username and passwords for database connection require 'db_conf'; #create the CGI object my $put_dbCGI = new CGI; #connect to our database my $dbh = DBI->connect($dsn, $username, $password); #grab the data from the submitted FORM my $name = $put_dbCGI->param('name'); my $sql = "INSERT INTO put_db (name) VALUES ('$name');"; my $sth = $dbh->prepare("$sql"); if (!$sth) { die "Error:" . $dbh->errstr . "\n"; } if (!$sth->execute) { die "Error:" . $sth->errstr . "\n"; } $sth->finish(); #grab all the data to show that it was successful $sql = "SELECT * FROM put_db;"; $sth = $dbh->prepare("$sql"); if (!$sth) { die "Error:" . $dbh->errstr . "\n"; } if (!$sth->execute) { die "Error:" . $sth->errstr . "\n"; } my @data; while (my $ref = $sth->fetchrow_arrayref) { push(@data, "$$ref[0],$$ref[1]"); } $sth->finish(); print $put_dbCGI->header; print <put_db.cgi Success Success.
Data in the put_db table:
EOF foreach (@data) { print "
"; print " "; } print <" . $_ . " "; print "EOF $dbh->disconnect; exit;