?
Solved

Simple CGI (perl) form processing script

Posted on 2003-03-21
3
Medium Priority
?
289 Views
Last Modified: 2013-12-25
This script inserts values from an html form into a database.  The problem is that I want it to fail if the $number value already exists in the database.  Is there a simple way of achieving this? Is this even the right area to ask this question?



#!/usr/bin/perl -w
use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser);
use DBI;
$ENV{"ORACLE_HOME"} = "/home/oracle/product/9.2.0";
print "Content-type: text/html\n\n";
$dbh = DBI->connect('dbi:Oracle:', q{gavin/a32182302@(DESCRIPTION=
  (ADDRESS=(PROTOCOL=TCP)(HOST= info200.infc.ulst.ac.uk)(PORT=1521))
  (CONNECT_DATA=(SID=ORCL)))}, "") or die "Bad connect";
my $name=param('EName');
my $number=param('ENumber');
my $salary=param('SalaryInfo');
my $job=param('jobType');
print "Inserting ...Ename=$name,Enumber=$number,SalaryInfo=$salary, Job=$job\n";  
my $sql =qq{insert into emp(empno,ename,sal,job) values(?,?,?,?)};
my $sth=$dbh->prepare($sql);
$sth->execute($number,$name,$salary,$job) or die $DBI::errstr;
print "</br>Insert Complete\n";
$dbh->disconnect;
0
Comment
Question by:henrycrinkle34
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 2

Accepted Solution

by:
Itatsumaki earned 300 total points
ID: 8184050
# Sure, plenty of ways to do this.  This is the easiest
# programmatically, but does leave your database vulnerable
# to race conditions.  If you are using oracle, why not
# just enable a primary or unique key on the DB table?

#!/usr/bin/perl -w
use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser);
use DBI;
$ENV{"ORACLE_HOME"} = "/home/oracle/product/9.2.0";
print "Content-type: text/html\n\n";
$dbh = DBI->connect('dbi:Oracle:', q{gavin/a32182302@(DESCRIPTION=
 (ADDRESS=(PROTOCOL=TCP)(HOST= info200.infc.ulst.ac.uk)(PORT=1521))
 (CONNECT_DATA=(SID=ORCL)))}, "") or die "Bad connect";
my $name=param('EName');
my $number=param('ENumber');
my $salary=param('SalaryInfo');
my $job=param('jobType');
print "Inserting ...Ename=$name,Enumber=$number,SalaryInfo=$salary, Job=$job\n";  
my $sql1 =qq{insert into emp(empno,ename,sal,job) values(?,?,?,?)};
my $sql2 = 'SELECT COUNT(*) FROM EMP WHERE empno = ?';
my $sth1=$dbh->prepare($sql1);
my $sth2=$dbh->prepare($sql2);

$sth2->execute($number);

if ($sth2->fetchrow_array() > 0) {
     $sth->execute($number,$name,$salary,$job) or die $DBI::errstr;
     print "</br>Insert Complete\n";
     }
else {
     print "</br>Insert Failed: duplicate record\n";
     }

$dbh->disconnect;

# Let me know if you have any problems with this.
# -tats
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question