This assumes MySQL is your DB. I know there are other people on this list
that can offer more efficient solutions or pick this one apart...so feel
free. :)

*******************************************************
#!/usr/bin/perl

use strict;
use CGI qw(:all);
use CGI::Carp qw(fatalsToBrowser);
use DBI;

my $q = new CGI;                  # new CGI object

my %formdata = $q->Vars;  # NV pairs, use caution though because
                                  # items with multiple values will have
                                  # be called with references.


# Let's assume that %formdata looks like this:
# %formdata = (name  => "Joe",
#                phone => "888-555-1212",
#                fax   => "888-555-1313");


# Connect to the database.
my $value = ""; # Have to declare variables if we use strict.

my $dbh =
DBI->connect("DBI:mysql:database_name:localhost","username","password") or
die "I could not connect to the database: $!";


# Prepare the SQL statement(s) for execution, then execute them.
foreach my $key(%formdata) {
 $value = $formdata{$key};
  my $sth = $dbh->prepare(qq|INSERT INTO
contactData(Firstname,PhoneNo,FaxNo)
                                     VALUES ($value,NULL,NULL)|) if($key eq "name");
  my $sth = $dbh->prepare(qq|INSERT INTO
contactData(Firstname,PhoneNo,FaxNo)
                                     VALUES (NULL,$value,NULL)|) if($key eq "phone");
  my $sth = $dbh->prepare(qq|INSERT INTO
contactData(Firstname,PhoneNo,FaxNo)
                                     VALUES (NULL,NULL,$value)|) if($key eq "fax");
  $sth->execute;
}


# Commit the changes.
$dbh->commit;


# Disconnect from the database.
$dbh->disconnect;
1;


-----
Scot Robnett
inSite Internet Solutions
[EMAIL PROTECTED]







-----Original Message-----
From: T. Murlidharan Nair [mailto:[EMAIL PROTECTED]
Sent: Monday, February 24, 2003 4:55 PM
To: [EMAIL PROTECTED]
Subject: NULL insertion


Hi!!
I need to insert NULL into the database using a perl CGI . So if a variable
is to be made NULL in perl and it needs to be used in an sql statement what
is best way to do it. Do I assign ' \N' to it or 'NULL' ?
Thanks and Cheers always!!
Murli





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to