#!C:\Perl\bin\perl.exe -wT ## use strict; use warnings; use CGI; use CGI::Carp qw/fatalsToBrowser/; use File::Basename; use DBI; my $driver = 'mysql'; my $database = 'mydatabase'; my $dsn = "DBI:$driver:database=$database"; my $userid = 'foo'; my $password = 'bar'; my $dbh = DBI->connect($dsn, $userid, $password )||die $DBI::errstr; my $query = CGI->new; ###################################################### ###################################################### #########This is where I generate the start number########## my $basisNumber = int(rand(999)); my $customerNumber = $basisNumber + 2000; ######################################################## ######################################################## #########Here I get ready to check if it's in the db######## my $customerCheck = $dbh->prepare("SELECT CustomerNumber FROM Customer +List"); $customerCheck->execute(); my(@customerMatrix)=(); while(my @customerArray = $customerCheck->fetchrow_array()) { push(@customerMatrix, [@customerArray]); } $customerCheck->finish(); ##################################################### ##################################################### ###Here I get ready to do the check in a subroutine####### my $checkResult = checkValue(@customerMatrix, $customerNumber); ####Here I check it, and increment it if it's not unique, then check a +gain######### while($checkResult==1) { $customerNumber ++; return $checkResult; } my $companyName = $query->param('cName'); my $billingStreetAddress = $query->param('bStreetAddress'); my $billingCity = $query->param('bCity'); my $billingState = $query->param('bState'); my $billingZip = $query->param('bZip'); my $billingPhone = $query->param('bPhone'); my $billingFax = $query->param('bFax'); my $shippingStreetAddress = $query->param('sStreetAddress'); my $shippingCity = $query->param('sCity'); my $shippingState = $query->param('sState'); my $shippingZip = $query->param('sZip'); my $shippingPhone = $query->param('sPhone'); my $shippingFax = $query->param('sFax'); my $email = $query->param('email'); my $contactPerson = $query->param('cPerson'); my $newCustomer = $dbh->prepare("INSERT INTO CustomerList (CustomerNum +ber, CompanyName, BillingStreetAddress, BillingCity, BillingState, Bi +llingZip, BillingPhone, BillingFax, ShippingStreetAddress, ShippingCi +ty, ShippingState, ShippingZip, ShippingPhone, ShippingFax, Email, Co +ntactPerson) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" +); $newCustomer->execute($customerNumber, $companyName, $billingStreetAdd +ress, $billingCity, $billingState, $billingZip, $billingPhone, $billi +ngFax, $shippingStreetAddress, $shippingCity, $shippingState, $shippi +ngZip, $shippingPhone, $shippingFax, $email, $contactPerson); $newCustomer->finish(); $dbh->commit; print $query->redirect('http://localhost/mysite.com/thankYou.html'); #################################################### #################################################### ########This is the subroutine that does the check###### sub checkValue { for(@customerMatrix) { return 1 if $_ eq $customerNumber; } return 0; }
In reply to DBI Insert Unique Customer ID by kalchas
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |