CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Need help writing a script that interacts with a MySQL DB

 

First page Previous page 1 2 Next page Last page  View All


MDTech.us_MAN
Novice

Sep 29, 2012, 1:37 PM

Post #1 of 45 (13302 views)
Need help writing a script that interacts with a MySQL DB Can't Post

Hello All!

I am currently learning Perl CGI using a book I have. I now want to write a script that receives a person's name as an argument then, looks up the person in the database and returns the Name and Email.

I need help with this part:

Code
my @results; 
while (@results = $query1->fetchrow_array()) {
foreach(@results) {
if ($index = "1") {
print "\nName: $_\n";
$index = "2"
} else {
if ($index = "2") {
print "\nEmail: $_\n";
$index = "2"
}
}
}
}



Here is what I already have:

Code
#!/usr/bin/perl 

use strict;
use DBI;


#my $username = 'root';
#my $dsn = 'DBI:mysql:mysql';
my $conn = DBI->connect('DBI:mysql:<DBName>;host=<ServerIP>', '<Username>', '<Password>', { RaiseError => 1 }) || die "Could not connect to the database";
my $console = $ARGV[0];


my $query = "SELECT * FROM People where Name = '$console'";
my $query1 = $conn->prepare($query) || die "Error preparing query" . $conn->errstr;


$query1->execute || die "Error executing query" . $query1->errstr;
print "$query";

my $index = "1";

my @results;
while (@results = $query1->fetchrow_array()) {
foreach(@results) {
if ($index = "1") {
print "\nName: $_\n";
$index = "2"
} else {
if ($index = "2") {
print "\nEmail: $_\n";
$index = "2"
}
}
}
}

if ($query1->rows == 0 ) {
print "No Records", "\n";
}


Maxwell D.


(This post was edited by MDTech.us_MAN on Sep 29, 2012, 1:54 PM)


wickedxter
User

Sep 29, 2012, 7:05 PM

Post #2 of 45 (13283 views)
Re: [MDTech.us_MAN] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post


Code
@driver_names = DBI->available_drivers; 
%drivers = DBI->installed_drivers;


check these to make sure that MySQL module is installed which would be DBD::mysql module if not youll need to install it. Is it connecting to the database correctly? also the $console sometimes is better to use an ? (alias)


Code
#use or not || when doing error check for statements like these. 

my $query = "SELECT * FROM People where Name = '?'";
my $query1 = $conn->prepare($query) or die "Error preparing query" . $conn->errstr;

$query1->execute($console) or die "Error executing query" . $query1->errstr;



Code
#try replaceing the while with this.  
while (my $results = $query1->fetchrow_hashref()) {
print $results->{name} . "\n";
print $results->{email} . "\n\n";
}



MDTech.us_MAN
Novice

Sep 30, 2012, 8:15 PM

Post #3 of 45 (13261 views)
Re: [wickedxter] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post

OK, thanks wickedxter for your code!

Now I have NEW problems :(

On line 39:

Code
$FName = $results->{FName};


Here is what I have so far: (including the HTML reply)

Code
#!/usr/bin/perl 

#use strict;
use DBI;

print "Content-type: text/html\n\n";

# Get the name and value for each form input:
@pairs = split(/&/, $ENV{'QUERY_STRING'});
# Then for each name/value pair....
foreach $pair (@pairs) {
# Separate the name and value:
($name, $value) = split(/=/, $pair);
# Convert + signs to spaces:
$value =~ tr/+/ /;
# Convert hex pairs (%HH) to ASCII characters:
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
# Store values in a hash called %FORM:
$FORM{$name} = $value;
}

$username = 'root';
$dsn = 'DBI:mysql:mysql';
$conn = DBI->connect('DBI:mysql:bcpadb;host=208.11.220.249', 'maxwell175', 'qwertyuiop', { RaiseError => 1 }) || die "Could not connect to the database";
$LName = $FORM{'LName'};
$FName = $FORM{'FName'};

TRYAGAIN:
$query = "SELECT * FROM People2 WHERE LName LIKE '$LName' AND FName LIKE '$FName'";
$query1 = $conn->prepare($query) || die "Error preparing query" . $conn->errstr;


$query1->execute || die "Error executing 'select' query: " . $query1->errstr;
#print "$query";

#try replaceing the while with this.
$results = $query1->fetchrow_hashref()

$FName = $results->{FName};
$LName = $results->{LName};
$AddrLine1 = $results->{AddrLine1};
$AddrLine2 = $results->{AddrLine2};
$City = $results->{City};
$Zip = $results->{Zip};
$WAddrLine1 = = $results->{WAddrLine1};
$WAddrLine2 = $results->{WAddrLine2};
$WCity = $results->{WCity};
$WZip = $results->{WZip};
$HPhone = $results->{HPhone};
$MPhone = $results->{MPhone};
$WPhone = $results->{WPhone};
$Email = $results->{Email};
#}


print <<HTMLPAGE;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv='X-UA-Compatible' content='IE=EmulateIE7' />
<title>BCPA page - MDTech</title>

<meta name="msvalidate.01" content="6698DB3266240D800A1DEFB2AEE5249D" />
<meta name="msvalidate.01" content="BA291C2BB06D4C099CF9138D94C4C24B" />
<meta name="google-site-verification" content="Mv8ukVrmYK4zMgdnRp4qYETNyRnPyRtaI-YEzYjDzV0" />
<meta name="msvalidate.01" content="BA291C2BB06D4C099CF9138D94C4C24B" />
<meta name="google-site-verification" content="Mv8ukVrmYK4zMgdnRp4qYETNyRnPyRtaI-YEzYjDzV0" />
<!--[if IE 7]>
<style>
p {
line-height:18px;
padding-bottom:0px;
}
h1 {
font-size:20px;
font-weight:bold;
color:#272e34;
padding-bottom:0px;
}
h2 {
font-size:14px;
font-weight:bold;
color:#272e34;
padding-bottom:0px;
}
</style>
<![endif]-->
<meta name="google-site-verification" content="Mv8ukVrmYK4zMgdnRp4qYETNyRnPyRtaI-YEzYjDzV0" />
<meta name="msvalidate.01" content="BA291C2BB06D4C099CF9138D94C4C24B" />
<meta name="google-translate-customization" content="4870ff0762da6435-906b3721014154a0-g256d826ced58f04d-9"></meta>

<link rel='stylesheet' type='text/css' href='http://cdn1.editmysite.com/editor/libraries/fancybox/fancybox.css?1348176505'></script>
<link rel='stylesheet' href='http://cdn1.editmysite.com/editor/images/common/common-v2.css?buildTime=1348176505' type='text/css' />
<link rel='stylesheet' type='text/css' href='files/main_style.css?1348197132' title='wsite-theme-css' />
<style type='text/css'>
#wsite-content div.paragraph, #wsite-content p, #wsite-content .product-block .product-title, #wsite-content .product-description, .blog-sidebar div.paragraph, .blog-sidebar p, .wsite-form-field label, .wsite-form-field label {}
#wsite-content h2, #wsite-content .product-long .product-title, #wsite-content .product-large .product-title, #wsite-content .product-small .product-title, .blog-sidebar h2 {}
#wsite-title{}
</style>
<script type='text/javascript'><!--
var STATIC_BASE = 'http://cdn1.editmysite.com/';
var STYLE_PREFIX = 'wsite';
//-->
</script>
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script type='text/javascript' src='http://cdn1.editmysite.com/editor/libraries/jquery_effects.js?1348176505'></script>
<script type='text/javascript' src='http://cdn1.editmysite.com/editor/libraries/fancybox/fancybox.min.js?1348176505'></script>
<script type='text/javascript' src='http://cdn1.editmysite.com/editor/images/common/utilities-jq.js?1348176505'></script>
<script type='text/javascript' src='http://cdn1.editmysite.com/editor/libraries/flyout_menus_jq.js?1348176505'></script>
<script type='text/javascript'><!--
var IS_ARCHIVE=1;
(function(jQuery){
function initFlyouts(){initPublishedFlyoutMenus([{"id":"732580610550224581","title":"Home","url":"index.html"},{"id":"791199019507589116","title":"Software","url":"software.html"},{"id":"185665631450527889","title":"Contact Us","url":"contact-us.html"},{"id":"655691623107481862","title":"Register","url":"register.html"},{"id":"120377591448869403","title":"Windows 8 HOW-TOs","url":"windows-8-how-tos.html"},{"id":"383720836856858449","title":"HTTPS Mirror INFO","url":"https-mirror-info.html"},{"id":"970940401954682759","title":"How to install ooVoo","url":"how-to-install-oovoo.html"},{"id":"925835062994562531","title":"Subscribe for MDTech.us Newsletter","url":"subscribe-for-mdtechus-newsletter.html"},{"id":"664166098389600041","title":"MDT Certificate Authority","url":"mdt-certificate-authority.html"},{"id":"939902576679924408","title":"Visitor Map","url":"visitor-map.html"},{"id":"517608385647474105","title":"Install Teamviewer","url":"install-teamviewer.html"}],'590082422623526219',"<li class='wsite-nav-more'><a href='#'>more...<\/a><\/li>",'',false)}
if (jQuery) {
if (jQuery.browser.msie) window.onload = initFlyouts;
else jQuery(initFlyouts)
}else{
if (Prototype.Browser.IE) window.onload = initFlyouts;
else document.observe('dom:loaded', initFlyouts);
}
})(window._W && _W.jQuery)
//-->
</script>
</head>
<body class='wsite-theme-light wsite-page-bcpa-page'>
<div id="wrapper">
<div id="header">
<div id="sitename"><span id="wsite-title">MDTech</span></div>
</div>
<div id="navigation">
<div id="navigation"><ul><li id='pg732580610550224581'><a href='index.html'>Home</a></li><li id='pg791199019507589116'><a href='software.html'>Software</a></li></ul></div>
</div>
<div id="content-wrapper">
<div id="contents-body">
<div id="contents"><div id='wsite-content' class='wsite-not-footer'>
<h1>Edit your information and click &quot;Submit&quot;:</h1>
<form action="/BCPA/BCPA_form/PROC/done/" method="get">
First Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Last Name:<br />
<input type=text name="FName" value="$FName" style="height: 23px; width: 133px" readonly="readonly" />
<input type=text name="LName" value="$LName" style="height: 23px; width: 133px" readonly="readonly" /><br />
Email:
<br />
<input type="text" name="Email" value="$Email" style="height: 23px; width: 239px" />
<br /><br />
<h4>Your Address:</h4>
Address Line 1:<br />
<input type="text" name="AddrLine1" value="$AddrLine1" style="height: 23px; width: 239px" />
<br />Address Line 2:<br />
<input type="text" name="AddrLine2" value="$AddrLine2" style="height: 23px; width: 239px" />
<br />City:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Zip:<br />
<input type="text" name="City" value="$City" style="height: 23px; width: 125px" />&nbsp;&nbsp;&nbsp;
<input type="text" name="Zip" value="$Zip" style="height: 23px; width: 63px" /> (state not needed)
<br /><br /><br />
<h4>Work Info:</h4>
Company:<br />
<input type="text" name="Company" value="$Company" style="height: 23px; width: 125px" />
<br />
Job Title:<br />
<input type="text" name="Job_Title" value="$Job_Title" style="height: 23px; width: 156px" />
<br /><br />
<font size="2"><b>Work Address:</b></font><br /><br />
Address Line 1:<br />
<input type="text" name="AddrLine1" value="$WAddrLine1" style="height: 23px; width: 239px" />
<br />Address Line 2:<br />
<input type="text" name="AddrLine2" value="$WAddrLine2" style="height: 23px; width: 239px" />
<br />City:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Zip:<br />
<input type="text" name="City" value="$WCity" style="height: 23px; width: 125px" />&nbsp;&nbsp;&nbsp;
<input type="text" name="Zip" value="$WZip" style="height: 23px; width: 63px" /> (state not needed)
<br /><br />
<h4>Phone numbers:</h4>
Home Phone:<br /><input type="text" name="HPhone" value="$HPhone" style="height: 23px; width: 125px" /><br />
Mobile Phone:<br /><input type="text" name="MPhone" value="$MPhone" style="height: 23px; width: 125px" /><br />
Work Phone:<br /><input type="text" name="WPhone" value="$WPhone" style="height: 23px; width: 125px" /><br />
<br /><br />
<input type="button" action="submit" value="Submit" />
</form>
</div>
</div>
<div id="contents-bottom"></div>

</div>
</div>

<meta name="google-site-verification" content="Mv8ukVrmYK4zMgdnRp4qYETNyRnPyRtaI-YEzYjDzV0" />
<meta name="msvalidate.01" content="BA291C2BB06D4C099CF9138D94C4C24B" />

</body>
</html>
HTMLPAGE

exit;



(This post was edited by MDTech.us_MAN on Oct 1, 2012, 7:36 AM)


MDTech.us_MAN
Novice

Oct 1, 2012, 10:33 AM

Post #4 of 45 (13248 views)
Re: [wickedxter] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post

Here is the code without the HTML:


Code
#!/usr/bin/perl 

use strict;
use DBI;


my $username = 'root';
my $dsn = 'DBI:mysql:mysql';
my $conn = DBI->connect('DBI:mysql:bcpadb;host=208.11.220.249', 'maxwell175', 'qwertyuiop', { RaiseError => 1 }) || die "Could not connect to the database";
my $LName = $ARGV[0];
my $FName = $ARGV[1];

TRYAGAIN:
my $query = "SELECT * FROM People2 WHERE LName LIKE '$LName' AND FName LIKE '$FName'";
my $query1 = $conn->prepare($query) || die "Error preparing query" . $conn->errstr;


$query1->execute || die "Error executing 'select' query: " . $query1->errstr;
#print "$query";

#try replaceing the while with this.
while (my $results = $query1->fetchrow_hashref()) {
print "\nFirst Name: " . $results->{FName} . "\n";
print "Last Name: " . $results->{LName} . "\n\n\n";
print "Address Line 1: " . $results->{AddrLine1} . "\n";
print "Address Line 2: " . $results->{AddrLine2} . "\n";
print "City: " . $results->{City} . "\n";
print "Zip: " . $results->{Zip} . "\n\n\n";
print "Email: " . $results->{Email} . "\n";
}

if ($query1->rows == 0 ) {
my $query = "INSERT INTO `People2` VALUES ('$FName','$LName','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A')";
my $query1 = $conn->prepare($query) || die "Error preparing query" . $conn->errstr;

$query1->execute || die "Error executing 'create' query" . $query1->errstr;

print "New record created sucessfully\n\n\n";

goto TRYAGAIN;
}



wickedxter
User

Oct 1, 2012, 12:01 PM

Post #5 of 45 (13246 views)
Re: [MDTech.us_MAN] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post

Post the error messages.... and are you running this mysql server on your computer or is it hosted?

I dont have access to an mysql server or want to install one so I used SQLite database.


Code
#!/usr/bin/perl  

use strict;
use DBI;


#my $username = 'root';
#my $dsn = 'DBI:mysql:mysql';
#my $conn = DBI->connect('DBI:mysql:bcpadb;host=208.11.220.249', 'maxwell175', 'qwertyuiop', { RaiseError => 1 }) or die "Could not connect to the database";

my $dbfile = 'C:/server/cgi-bin/tests/sqltest.sqlite';
my $conn = DBI->connect("dbi:SQLite:dbname=$dbfile", { RaiseError => 1 }) or die "Could not connect to the database";
my $first = $ARGV[0];
my $last = $ARGV[1];


my $query1 = $conn->prepare("SELECT * FROM people WHERE first LIKE ? AND last LIKE ?") or die "Error preparing query" . $conn->errstr;


$query1->execute($first,$last) or die "Error executing 'select' query: " . $query1->errstr;
#print "$query";

#try replaceing the while with this.
while (my $results = $query1->fetchrow_hashref()) {
print "\nFirst Name: " . $results->{first} . "\n";
print "Last Name: " . $results->{last} . "\n\n\n";
#print "Address Line 1: " . $results->{AddrLine1} . "\n";
#print "Address Line 2: " . $results->{AddrLine2} . "\n";
#print "City: " . $results->{City} . "\n";
#print "Zip: " . $results->{Zip} . "\n\n\n";
#print "Email: " . $results->{Email} . "\n";
}

if ($query1->rows == 0 ) {

my $query1 = $conn->prepare("INSERT INTO people VALUES (?,?)") or die "Error preparing query" . $conn->errstr;

$query1->execute($first,$last) or die "Error executing 'create' query" . $query1->errstr;

print "New record created sucessfully\n\n\n";


}



MDTech.us_MAN
Novice

Oct 1, 2012, 3:24 PM

Post #6 of 45 (13241 views)
Re: [MDTech.us_MAN] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post


In Reply To
OK, thanks wickedxter for your code!

Now I have NEW problems :(

On line 39:

Code
$FName = $results->{FName};


Here is what I have so far: (including the HTML reply)

Code
#!/usr/bin/perl 

#use strict;
use DBI;

print "Content-type: text/html\n\n";

# Get the name and value for each form input:
@pairs = split(/&/, $ENV{'QUERY_STRING'});
# Then for each name/value pair....
foreach $pair (@pairs) {
# Separate the name and value:
($name, $value) = split(/=/, $pair);
# Convert + signs to spaces:
$value =~ tr/+/ /;
# Convert hex pairs (%HH) to ASCII characters:
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
# Store values in a hash called %FORM:
$FORM{$name} = $value;
}

$username = 'root';
$dsn = 'DBI:mysql:mysql';
$conn = DBI->connect('DBI:mysql:bcpadb;host=208.11.220.249', 'maxwell175', 'qwertyuiop', { RaiseError => 1 }) || die "Could not connect to the database";
$LName = $FORM{'LName'};
$FName = $FORM{'FName'};

TRYAGAIN:
$query = "SELECT * FROM People2 WHERE LName LIKE '$LName' AND FName LIKE '$FName'";
$query1 = $conn->prepare($query) || die "Error preparing query" . $conn->errstr;


$query1->execute || die "Error executing 'select' query: " . $query1->errstr;
#print "$query";

#try replaceing the while with this.
$results = $query1->fetchrow_hashref()

$FName = $results->{FName};
$LName = $results->{LName};
$AddrLine1 = $results->{AddrLine1};
$AddrLine2 = $results->{AddrLine2};
$City = $results->{City};
$Zip = $results->{Zip};
$WAddrLine1 = = $results->{WAddrLine1};
$WAddrLine2 = $results->{WAddrLine2};
$WCity = $results->{WCity};
$WZip = $results->{WZip};
$HPhone = $results->{HPhone};
$MPhone = $results->{MPhone};
$WPhone = $results->{WPhone};
$Email = $results->{Email};
#}


print <<HTMLPAGE;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv='X-UA-Compatible' content='IE=EmulateIE7' />
<title>BCPA page - MDTech</title>

<meta name="msvalidate.01" content="6698DB3266240D800A1DEFB2AEE5249D" />
<meta name="msvalidate.01" content="BA291C2BB06D4C099CF9138D94C4C24B" />
<meta name="google-site-verification" content="Mv8ukVrmYK4zMgdnRp4qYETNyRnPyRtaI-YEzYjDzV0" />
<meta name="msvalidate.01" content="BA291C2BB06D4C099CF9138D94C4C24B" />
<meta name="google-site-verification" content="Mv8ukVrmYK4zMgdnRp4qYETNyRnPyRtaI-YEzYjDzV0" />
<!--[if IE 7]>
<style>
p {
line-height:18px;
padding-bottom:0px;
}
h1 {
font-size:20px;
font-weight:bold;
color:#272e34;
padding-bottom:0px;
}
h2 {
font-size:14px;
font-weight:bold;
color:#272e34;
padding-bottom:0px;
}
</style>
<![endif]-->
<meta name="google-site-verification" content="Mv8ukVrmYK4zMgdnRp4qYETNyRnPyRtaI-YEzYjDzV0" />
<meta name="msvalidate.01" content="BA291C2BB06D4C099CF9138D94C4C24B" />
<meta name="google-translate-customization" content="4870ff0762da6435-906b3721014154a0-g256d826ced58f04d-9"></meta>

<link rel='stylesheet' type='text/css' href='http://cdn1.editmysite.com/editor/libraries/fancybox/fancybox.css?1348176505'></script>
<link rel='stylesheet' href='http://cdn1.editmysite.com/editor/images/common/common-v2.css?buildTime=1348176505' type='text/css' />
<link rel='stylesheet' type='text/css' href='files/main_style.css?1348197132' title='wsite-theme-css' />
<style type='text/css'>
#wsite-content div.paragraph, #wsite-content p, #wsite-content .product-block .product-title, #wsite-content .product-description, .blog-sidebar div.paragraph, .blog-sidebar p, .wsite-form-field label, .wsite-form-field label {}
#wsite-content h2, #wsite-content .product-long .product-title, #wsite-content .product-large .product-title, #wsite-content .product-small .product-title, .blog-sidebar h2 {}
#wsite-title{}
</style>
<script type='text/javascript'><!--
var STATIC_BASE = 'http://cdn1.editmysite.com/';
var STYLE_PREFIX = 'wsite';
//-->
</script>
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script type='text/javascript' src='http://cdn1.editmysite.com/editor/libraries/jquery_effects.js?1348176505'></script>
<script type='text/javascript' src='http://cdn1.editmysite.com/editor/libraries/fancybox/fancybox.min.js?1348176505'></script>
<script type='text/javascript' src='http://cdn1.editmysite.com/editor/images/common/utilities-jq.js?1348176505'></script>
<script type='text/javascript' src='http://cdn1.editmysite.com/editor/libraries/flyout_menus_jq.js?1348176505'></script>
<script type='text/javascript'><!--
var IS_ARCHIVE=1;
(function(jQuery){
function initFlyouts(){initPublishedFlyoutMenus([{"id":"732580610550224581","title":"Home","url":"index.html"},{"id":"791199019507589116","title":"Software","url":"software.html"},{"id":"185665631450527889","title":"Contact Us","url":"contact-us.html"},{"id":"655691623107481862","title":"Register","url":"register.html"},{"id":"120377591448869403","title":"Windows 8 HOW-TOs","url":"windows-8-how-tos.html"},{"id":"383720836856858449","title":"HTTPS Mirror INFO","url":"https-mirror-info.html"},{"id":"970940401954682759","title":"How to install ooVoo","url":"how-to-install-oovoo.html"},{"id":"925835062994562531","title":"Subscribe for MDTech.us Newsletter","url":"subscribe-for-mdtechus-newsletter.html"},{"id":"664166098389600041","title":"MDT Certificate Authority","url":"mdt-certificate-authority.html"},{"id":"939902576679924408","title":"Visitor Map","url":"visitor-map.html"},{"id":"517608385647474105","title":"Install Teamviewer","url":"install-teamviewer.html"}],'590082422623526219',"<li class='wsite-nav-more'><a href='#'>more...<\/a><\/li>",'',false)}
if (jQuery) {
if (jQuery.browser.msie) window.onload = initFlyouts;
else jQuery(initFlyouts)
}else{
if (Prototype.Browser.IE) window.onload = initFlyouts;
else document.observe('dom:loaded', initFlyouts);
}
})(window._W && _W.jQuery)
//-->
</script>
</head>
<body class='wsite-theme-light wsite-page-bcpa-page'>
<div id="wrapper">
<div id="header">
<div id="sitename"><span id="wsite-title">MDTech</span></div>
</div>
<div id="navigation">
<div id="navigation"><ul><li id='pg732580610550224581'><a href='index.html'>Home</a></li><li id='pg791199019507589116'><a href='software.html'>Software</a></li></ul></div>
</div>
<div id="content-wrapper">
<div id="contents-body">
<div id="contents"><div id='wsite-content' class='wsite-not-footer'>
<h1>Edit your information and click &quot;Submit&quot;:</h1>
<form action="/BCPA/BCPA_form/PROC/done/" method="get">
First Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Last Name:<br />
<input type=text name="FName" value="$FName" style="height: 23px; width: 133px" readonly="readonly" />
<input type=text name="LName" value="$LName" style="height: 23px; width: 133px" readonly="readonly" /><br />
Email:
<br />
<input type="text" name="Email" value="$Email" style="height: 23px; width: 239px" />
<br /><br />
<h4>Your Address:</h4>
Address Line 1:<br />
<input type="text" name="AddrLine1" value="$AddrLine1" style="height: 23px; width: 239px" />
<br />Address Line 2:<br />
<input type="text" name="AddrLine2" value="$AddrLine2" style="height: 23px; width: 239px" />
<br />City:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Zip:<br />
<input type="text" name="City" value="$City" style="height: 23px; width: 125px" />&nbsp;&nbsp;&nbsp;
<input type="text" name="Zip" value="$Zip" style="height: 23px; width: 63px" /> (state not needed)
<br /><br /><br />
<h4>Work Info:</h4>
Company:<br />
<input type="text" name="Company" value="$Company" style="height: 23px; width: 125px" />
<br />
Job Title:<br />
<input type="text" name="Job_Title" value="$Job_Title" style="height: 23px; width: 156px" />
<br /><br />
<font size="2"><b>Work Address:</b></font><br /><br />
Address Line 1:<br />
<input type="text" name="AddrLine1" value="$WAddrLine1" style="height: 23px; width: 239px" />
<br />Address Line 2:<br />
<input type="text" name="AddrLine2" value="$WAddrLine2" style="height: 23px; width: 239px" />
<br />City:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Zip:<br />
<input type="text" name="City" value="$WCity" style="height: 23px; width: 125px" />&nbsp;&nbsp;&nbsp;
<input type="text" name="Zip" value="$WZip" style="height: 23px; width: 63px" /> (state not needed)
<br /><br />
<h4>Phone numbers:</h4>
Home Phone:<br /><input type="text" name="HPhone" value="$HPhone" style="height: 23px; width: 125px" /><br />
Mobile Phone:<br /><input type="text" name="MPhone" value="$MPhone" style="height: 23px; width: 125px" /><br />
Work Phone:<br /><input type="text" name="WPhone" value="$WPhone" style="height: 23px; width: 125px" /><br />
<br /><br />
<input type="button" action="submit" value="Submit" />
</form>
</div>
</div>
<div id="contents-bottom"></div>

</div>
</div>

<meta name="google-site-verification" content="Mv8ukVrmYK4zMgdnRp4qYETNyRnPyRtaI-YEzYjDzV0" />
<meta name="msvalidate.01" content="BA291C2BB06D4C099CF9138D94C4C24B" />

</body>
</html>
HTMLPAGE

exit;



Check my post above ----^


FishMonger
Veteran / Moderator

Oct 2, 2012, 7:19 AM

Post #7 of 45 (13226 views)
Re: [MDTech.us_MAN] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post

You should always tell us what errors you're receiving so that we can better assist you.

Why did you comment out the use strict; line? It's very important to include that in every Perl script you write so that it can point out some of your mistakes. It's common for people on forums like this to not help unless you include the strict pragma in your code.

Two mistakes that you made are 1) line 37 is missing the terminating semicolon and 2) line 45 has an extra = equals character.

Lines 6 thru 20 should be thrown out and replaced with this:

Code
use CGI; 

my $cgi = CGI->new;
my %FORM = $cgi->Vars;

print $cgi->header;

http://search.cpan.org/~markstos/CGI.pm-3.60/lib/CGI.pm

All of your vars should be declared with the 'my' keyword, like I just showed.

Your html code needs to be cleaned up and reformatted. Having lines over 1000 characters long is absurd.

Instead of using the here document for the html, it would be much cleaner/better to put that into a separate file which is loaded via the HTML::Template module.
http://search.cpan.org/~wonko/HTML-Template-2.91/lib/HTML/Template.pm


MDTech.us_MAN
Novice

Oct 2, 2012, 12:27 PM

Post #8 of 45 (13212 views)
Re: [FishMonger] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post

Hello,

Thanks for the reply!

1) here is the error I get I after I "used" strict:

Code
Global symbol "$Company" requires explicit package name at BCPA_form_PROC.cgi line 46. 
Global symbol "$Job_Title" requires explicit package name at BCPA_form_PROC.cgi line 46.
Execution of BCPA_form_PROC.cgi aborted due to compilation errors.


2) Does the HTML::Template module process variable references? (for example $LName)

3) The "theme" HTML is generate by the Weebly website editor (further edited by me).

4) The MySQL server is hosted on freeMySQL.net

5) Here is the new code:

Code
#!/usr/bin/perl 

use strict;
use DBI;
use CGI;

my $cgi = CGI->new;
my %FORM = $cgi->Vars;

print $cgi->header;

my $username = 'root';
my $dsn = 'DBI:mysql:mysql';
my $conn = DBI->connect('DBI:mysql:bcpadb;host=<serverIP>', '<username>', '<password>', { RaiseError => 1 }) || die "Could not connect to the database";
my $LName = $FORM{'LName'};
my $FName = $FORM{'FName'};

TRYAGAIN:
my $query = "SELECT * FROM People2 WHERE LName LIKE '$LName' AND FName LIKE '$FName'";
my $query1 = $conn->prepare($query) || die "Error preparing query" . $conn->errstr;


my $query1->execute || die "Error executing 'select' query: " . $query1->errstr;
#print "$query";

#try replaceing the while with this.
my $results = $query1->fetchrow_hashref();

my $FName = $results->{FName};
my $LName = $results->{LName};
my $AddrLine1 = $results->{AddrLine1};
my $AddrLine2 = $results->{AddrLine2};
my $City = $results->{City};
my $Zip = $results->{Zip};
my $WAddrLine1 = $results->{WAddrLine1};
my $WAddrLine2 = $results->{WAddrLine2};
my $WCity = $results->{WCity};
my $WZip = $results->{WZip};
my $HPhone = $results->{HPhone};
my $MPhone = $results->{MPhone};
my $WPhone = $results->{WPhone};
my $Email = $results->{Email};
#}


print <<HTMLPAGE;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv='X-UA-Compatible' content='IE=EmulateIE7' />
<title>BCPA page - MDTech</title>

<meta name="msvalidate.01" content="6698DB3266240D800A1DEFB2AEE5249D" />
<meta name="msvalidate.01" content="BA291C2BB06D4C099CF9138D94C4C24B" />
<meta name="google-site-verification" content="Mv8ukVrmYK4zMgdnRp4qYETNyRnPyRtaI-YEzYjDzV0" />
<meta name="msvalidate.01" content="BA291C2BB06D4C099CF9138D94C4C24B" />
<meta name="google-site-verification" content="Mv8ukVrmYK4zMgdnRp4qYETNyRnPyRtaI-YEzYjDzV0" />
<!--[if IE 7]>
<style>
p {
line-height:18px;
padding-bottom:0px;
}
h1 {
font-size:20px;
font-weight:bold;
color:#272e34;
padding-bottom:0px;
}
h2 {
font-size:14px;
font-weight:bold;
color:#272e34;
padding-bottom:0px;
}
</style>
<![endif]-->
<meta name="google-site-verification" content="Mv8ukVrmYK4zMgdnRp4qYETNyRnPyRtaI-YEzYjDzV0" />
<meta name="msvalidate.01" content="BA291C2BB06D4C099CF9138D94C4C24B" />
<meta name="google-translate-customization" content="4870ff0762da6435-906b3721014154a0-g256d826ced58f04d-9"></meta>

<link rel='stylesheet' type='text/css' href='http://cdn1.editmysite.com/editor/libraries/fancybox/fancybox.css?1348176505'></script>
<link rel='stylesheet' href='http://cdn1.editmysite.com/editor/images/common/common-v2.css?buildTime=1348176505' type='text/css' />
<link rel='stylesheet' type='text/css' href='files/main_style.css?1348197132' title='wsite-theme-css' />
<style type='text/css'>
#wsite-content div.paragraph, #wsite-content p, #wsite-content .product-block .product-title, #wsite-content .product-description, .blog-sidebar div.paragraph, .blog-sidebar p, .wsite-form-field label, .wsite-form-field label {}
#wsite-content h2, #wsite-content .product-long .product-title, #wsite-content .product-large .product-title, #wsite-content .product-small .product-title, .blog-sidebar h2 {}
#wsite-title{}
</style>
<script type='text/javascript'><!--
var STATIC_BASE = 'http://cdn1.editmysite.com/';
var STYLE_PREFIX = 'wsite';
//-->
</script>
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script type='text/javascript' src='http://cdn1.editmysite.com/editor/libraries/jquery_effects.js?1348176505'></script>
<script type='text/javascript' src='http://cdn1.editmysite.com/editor/libraries/fancybox/fancybox.min.js?1348176505'></script>
<script type='text/javascript' src='http://cdn1.editmysite.com/editor/images/common/utilities-jq.js?1348176505'></script>
<script type='text/javascript' src='http://cdn1.editmysite.com/editor/libraries/flyout_menus_jq.js?1348176505'></script>
<script type='text/javascript'><!--
var IS_ARCHIVE=1;
(function(jQuery){
function initFlyouts(){initPublishedFlyoutMenus([{"id":"732580610550224581","title":"Home","url":"index.html"},{"id":"791199019507589116","title":"Software","url":"software.html"},{"id":"185665631450527889","title":"Contact Us","url":"contact-us.html"},{"id":"655691623107481862","title":"Register","url":"register.html"},{"id":"120377591448869403","title":"Windows 8 HOW-TOs","url":"windows-8-how-tos.html"},{"id":"383720836856858449","title":"HTTPS Mirror INFO","url":"https-mirror-info.html"},{"id":"970940401954682759","title":"How to install ooVoo","url":"how-to-install-oovoo.html"},{"id":"925835062994562531","title":"Subscribe for MDTech.us Newsletter","url":"subscribe-for-mdtechus-newsletter.html"},{"id":"664166098389600041","title":"MDT Certificate Authority","url":"mdt-certificate-authority.html"},{"id":"939902576679924408","title":"Visitor Map","url":"visitor-map.html"},{"id":"517608385647474105","title":"Install Teamviewer","url":"install-teamviewer.html"}],'590082422623526219',"<li class='wsite-nav-more'><a href='#'>more...<\/a><\/li>",'',false)}
if (jQuery) {
if (jQuery.browser.msie) window.onload = initFlyouts;
else jQuery(initFlyouts)
}else{
if (Prototype.Browser.IE) window.onload = initFlyouts;
else document.observe('dom:loaded', initFlyouts);
}
})(window._W && _W.jQuery)
//-->
</script>
</head>
<body class='wsite-theme-light wsite-page-bcpa-page'>
<div id="wrapper">
<div id="header">
<div id="sitename"><span id="wsite-title">MDTech</span></div>
</div>
<div id="navigation">
<div id="navigation"><ul><li id='pg732580610550224581'><a href='index.html'>Home</a></li><li id='pg791199019507589116'><a href='software.html'>Software</a></li></ul></div>
</div>
<div id="content-wrapper">
<div id="contents-body">
<div id="contents"><div id='wsite-content' class='wsite-not-footer'>
<h1>Edit your information and click &quot;Submit&quot;:</h1>
<form action="/BCPA/BCPA_form/PROC/done/" method="get">
First Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Last Name:<br />
<input type=text name="FName" value="$FName" style="height: 23px; width: 133px" readonly="readonly" />
<input type=text name="LName" value="$LName" style="height: 23px; width: 133px" readonly="readonly" /><br />
Email:
<br />
<input type="text" name="Email" value="$Email" style="height: 23px; width: 239px" />
<br /><br />
<h4>Your Address:</h4>
Address Line 1:<br />
<input type="text" name="AddrLine1" value="$AddrLine1" style="height: 23px; width: 239px" />
<br />Address Line 2:<br />
<input type="text" name="AddrLine2" value="$AddrLine2" style="height: 23px; width: 239px" />
<br />City:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Zip:<br />
<input type="text" name="City" value="$City" style="height: 23px; width: 125px" />&nbsp;&nbsp;&nbsp;
<input type="text" name="Zip" value="$Zip" style="height: 23px; width: 63px" /> (state not needed)
<br /><br /><br />
<h4>Work Info:</h4>
Company:<br />
<input type="text" name="Company" value="$Company" style="height: 23px; width: 125px" />
<br />
Job Title:<br />
<input type="text" name="Job_Title" value="$Job_Title" style="height: 23px; width: 156px" />
<br /><br />
<font size="2"><b>Work Address:</b></font><br /><br />
Address Line 1:<br />
<input type="text" name="AddrLine1" value="$WAddrLine1" style="height: 23px; width: 239px" />
<br />Address Line 2:<br />
<input type="text" name="AddrLine2" value="$WAddrLine2" style="height: 23px; width: 239px" />
<br />City:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Zip:<br />
<input type="text" name="City" value="$WCity" style="height: 23px; width: 125px" />&nbsp;&nbsp;&nbsp;
<input type="text" name="Zip" value="$WZip" style="height: 23px; width: 63px" /> (state not needed)
<br /><br />
<h4>Phone numbers:</h4>
Home Phone:<br /><input type="text" name="HPhone" value="$HPhone" style="height: 23px; width: 125px" /><br />
Mobile Phone:<br /><input type="text" name="MPhone" value="$MPhone" style="height: 23px; width: 125px" /><br />
Work Phone:<br /><input type="text" name="WPhone" value="$WPhone" style="height: 23px; width: 125px" /><br />
<br /><br />
<input type="button" action="submit" value="Submit" />
</form>
</div>
</div>
<div id="contents-bottom"></div>

</div>
</div>

<meta name="google-site-verification" content="Mv8ukVrmYK4zMgdnRp4qYETNyRnPyRtaI-YEzYjDzV0" />
<meta name="msvalidate.01" content="BA291C2BB06D4C099CF9138D94C4C24B" />

</body>
</html>
HTMLPAGE

exit;


Maxwell.


wickedxter
User

Oct 2, 2012, 12:41 PM

Post #9 of 45 (13210 views)
Re: [MDTech.us_MAN] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post

thats because they are defined in your HTML and Perl is complaining it cant find them. If they are not coming from the database then.


Code
my $FName = $results->{FName};   
my $LName = $results->{LName};
my $AddrLine1 = $results->{AddrLine1};
my $AddrLine2 = $results->{AddrLine2};
my $City = $results->{City};
my $Zip = $results->{Zip};
my $WAddrLine1 = $results->{WAddrLine1};
my $WAddrLine2 = $results->{WAddrLine2};
my $WCity = $results->{WCity};
my $WZip = $results->{WZip};
my $HPhone = $results->{HPhone};
my $MPhone = $results->{MPhone};
my $WPhone = $results->{WPhone};
my $Email = $results->{Email};

#Add these to get rid of the error##########
my $Company = $results->{Company};
my $Job_Title = $results->{Job_Title};



(This post was edited by wickedxter on Oct 2, 2012, 12:46 PM)


FishMonger
Veteran / Moderator

Oct 2, 2012, 6:08 PM

Post #10 of 45 (13199 views)
Re: [MDTech.us_MAN] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post


Quote
1) here is the error I get I after I "used" strict:

wickedxter addressed that issue.



Quote
2) Does the HTML::Template module process variable references? (for example $LName)

You need to assign the value to a param of the HTML::Template object.

Code
$template->param(LName => $results->{LName});


The corresponding line in the template file would be:

Code
<input type=text name="LName" value="<TMPL_VAR LName>" style="height: 23px; width: 133px" readonly="readonly" /><br />




Quote
3) The "theme" HTML is generate by the Weebly website editor (further edited by me).

HTML editors always produces poorly formatted code and in most cases introduce non standard "custom" code. It's much better to use a plain text editor and hand code the html.



Quote
4) The MySQL server is hosted on freeMySQL.net

Did you have a specific question or concern about that fact?



Quote
5) Here is the new code:

There are some issues I see with that code. Do you have a specific question or problem regarding that code which you want help on?


MDTech.us_MAN
Novice

Oct 5, 2012, 8:03 AM

Post #11 of 45 (12921 views)
Re: [FishMonger] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post


In Reply To
...


Quote
2) Does the HTML::Template module process variable references? (for example $LName)

You need to assign the value to a param of the HTML::Template object.

Code
$template->param(LName => $results->{LName});



Is it possible to pass multiple params?


In Reply To

Quote
3) The "theme" HTML is generate by the Weebly website editor (further edited by me).

HTML editors always produces poorly formatted code and in most cases introduce non standard "custom" code. It's much better to use a plain text editor and hand code the html.

I will do that when I will learn how to make drop-down menus in HTML and when I learn CSS.


In Reply To

Quote
4) The MySQL server is hosted on freeMySQL.net

Did you have a specific question or concern about that fact?

wickedxter asked.

I applied the fixes wickedxter suggested and now, I get the following problem:

Code
Can't call method "execute" on an undefined value at BCPA_form_PROC.cgi line 23.


Here is the whole code:
(paste bin link)
http://pastebin.com/fGhbHZKZ
(EDIT: link deleted)

Maxwell D.


(This post was edited by MDTech.us_MAN on Oct 14, 2012, 11:45 AM)


FishMonger
Veteran / Moderator

Oct 5, 2012, 8:02 PM

Post #12 of 45 (12885 views)
Re: [MDTech.us_MAN] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post


Quote
Is it possible to pass multiple params?

Yes. The module's documentation gives examples.
http://search.cpan.org/~wonko/HTML-Template-2.91/lib/HTML/Template.pm


Quote
Can't call method "execute" on an undefined value at BCPA_form_PROC.cgi line 23.

That's because you're overwriting the prior $query1 var declaration.

If you had used the warnings pragma, i.e.,

Code
use warnings;

these warnings would have been given.

Quote
"my" variable $query1 masks earlier declaration in same scope at Maxwell.pl line 23.
"my" variable $FName masks earlier declaration in same scope at Maxwell.pl line 29.
"my" variable $LName masks earlier declaration in same scope at Maxwell.pl line 30.

Remove the 'my' from each of those lines.


MDTech.us_MAN
Novice

Oct 12, 2012, 12:41 PM

Post #13 of 45 (12651 views)
Re: [FishMonger] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post


In Reply To

Quote
Is it possible to pass multiple params?

Yes. The module's documentation gives examples.
http://search.cpan.org/~wonko/HTML-Template-2.91/lib/HTML/Template.pm


Quote
Can't call method "execute" on an undefined value at BCPA_form_PROC.cgi line 23.

That's because you're overwriting the prior $query1 var declaration.

If you had used the warnings pragma, i.e.,

Code
use warnings;

these warnings would have been given.

Quote
"my" variable $query1 masks earlier declaration in same scope at Maxwell.pl line 23.
"my" variable $FName masks earlier declaration in same scope at Maxwell.pl line 29.
"my" variable $LName masks earlier declaration in same scope at Maxwell.pl line 30.

Remove the 'my' from each of those lines.


Ok,
I applied the fixes you said, but, now the CGI returns nothing via browser (but works via Command Prompt). Here is the url:
http://mdtech.us.ms/BCPA/BCPA_form/
Test Name: John Doe
Actual CGI url:
http://mdtech.us.ms/BCPA/BCPA_form/PROC/
To be exact: http://mdtech.us.ms/BCPA/BCPA_form/PROC/index.cgi

Full code:
http://pastebin.com/4jC1tgBD


wickedxter
User

Oct 12, 2012, 6:07 PM

Post #14 of 45 (12642 views)
Re: [MDTech.us_MAN] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post

why does your code have these <server ip> does this get replaced by you or by something else?


Code
#dont use ' ','' because if your useing '$server_ip' perl will not interpopulate the the scalar... 
my $conn = DBI->connect('DBI:mysql:bcpadb;host=<server IP>, '<username>', '<password>', { RaiseError => 1 }) || die "Could not connect to the database";

#should be
my $conn = DBI->connect("DBI:mysql:bcpadb;host=<server IP>", "<username>", "<password>", { RaiseError => 1 }) or die "Could not connect to the database";


#change this
my $LName = $FORM{'LName'};
my $FName = $FORM{'FName'};

#to this
my $LName = $FORM{LName};
my $FName = $FORM{FName};



(This post was edited by wickedxter on Oct 12, 2012, 6:29 PM)


FishMonger
Veteran / Moderator

Oct 13, 2012, 8:48 AM

Post #15 of 45 (12582 views)
Re: [MDTech.us_MAN] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post

Did you check the web server error log to see if it gives you a clue to the problem?

While debugging CGI scripts, you should include this module:
http://search.cpan.org/~markstos/CGI.pm-3.60/lib/CGI/Carp.pm

Code
use CGI::Carp qw(fatalsToBrowser);


You have a syntax error on line 15.

D:\test>perl -c MDTech.us_MAN.pl

Quote
syntax error at MDTech.us_MAN.pl line 15, near "username>"
Bad name after LName' at MDTech.us_MAN.pl line 16.


Line 15 is missing the closing ' on the DSN portion of the statement and could very well be the source of blank page problem.

IMO, you should use double quotes when you need interpolation and single quotes when you don't need interpolation. We don't have enough info from what you've posted to make a firm statement on which you should be using in this case.

I disagree with wickedxter on the quoting of the username, password, and hash keys. Use single quotes for the username/password or non at all if you're using vars. Hash keys rarely ever need quoting. Two exceptions are when they include spaces or hyphens.

Since you're using RaiseError => 1 in the connect statement, adding the die statement on the rest of the sql statements is redundant and unnecessary.

I'm tied up working on a PGA tournament, but if I get some free time, I'll post more recommendations.


FishMonger
Veteran / Moderator

Oct 13, 2012, 1:13 PM

Post #16 of 45 (12571 views)
Re: [MDTech.us_MAN] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post

Here's a rework of your script which uses the HTML::Template module to separate the code logic from the HTML presentation.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use DBI;
use CGI;
use HTML::Template;

print "Content-Type: text/html\n\n";

my $cgi = CGI->new;
my %form = $cgi->Vars;
my $LName = $form{LName};
my $FName = $form{FName};
my $template = HTML::Template->new(filename => 'test.tmpl');
my $dsn = 'DBI:mysql:bcpadb:<server IP>';
my $username = 'root';
my $password = 'password';

my $dbh = DBI->connect($dsn, $username, $password,
{ RaiseError => 1 }
) or die "Database connection failure: " . $DBI::errstr;

my $sql = "SELECT * FROM People2
WHERE LName LIKE ?
AND FName LIKE ?";

my $query = $dbh->prepare($sql);
$query->execute($LName, $FName);

my $results = $query->fetchrow_hashref();

foreach my $key ( keys %$results ) {
$template->param($key => $results->{$key});
}
$sth->finnish;
$dbh->disconnect;

$template->param(FName => $FName);
$template->param(LName => $LName);

$template->output;


The html would be in a separate file and the only changes needed would be to specify the template vars instead of the scalars.

e.g.,

Quote
value="$Company"

would become

Quote
value="<TMPL_VAR Company>"



MDTech.us_MAN
Novice

Oct 13, 2012, 8:10 PM

Post #17 of 45 (12561 views)
Re: [FishMonger] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post

Hello,

Thanks very much for the suggestion! that helped diagnose the problem, because I don't have access to the logs.

Here is the new problem:

Code
DBI connect('bcpadb;host=instance28854.db.xeround.com:18847','maxwell175',...) failed: Can't connect to MySQL server on 'instance28854.db.xeround.com' (111) at /home/www/mdtech.us.ms/BCPA/BCPA_form/PROC/index.cgi line 16


@wickedxter: I manually change <password> or <the password> to my actual db password. (not a variable)

Full code:
http://pastebin.com/iQYnTPUB






In Reply To
Did you check the web server error log to see if it gives you a clue to the problem?

While debugging CGI scripts, you should include this module:
http://search.cpan.org/~markstos/CGI.pm-3.60/lib/CGI/Carp.pm

Code
use CGI::Carp qw(fatalsToBrowser);


You have a syntax error on line 15.

D:\test>perl -c MDTech.us_MAN.pl

Quote
syntax error at MDTech.us_MAN.pl line 15, near "username>"
Bad name after LName' at MDTech.us_MAN.pl line 16.


Line 15 is missing the closing ' on the DSN portion of the statement and could very well be the source of blank page problem.

IMO, you should use double quotes when you need interpolation and single quotes when you don't need interpolation. We don't have enough info from what you've posted to make a firm statement on which you should be using in this case.

I disagree with wickedxter on the quoting of the username, password, and hash keys. Use single quotes for the username/password or non at all if you're using vars. Hash keys rarely ever need quoting. Two exceptions are when they include spaces or hyphens.

Since you're using RaiseError => 1 in the connect statement, adding the die statement on the rest of the sql statements is redundant and unnecessary.

I'm tied up working on a PGA tournament, but if I get some free time, I'll post more recommendations.



(This post was edited by MDTech.us_MAN on Oct 14, 2012, 11:47 AM)


FishMonger
Veteran / Moderator

Oct 14, 2012, 8:57 AM

Post #18 of 45 (12531 views)
Re: [MDTech.us_MAN] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post

Your connect statement doesn't include any error handling. Add the error handling (i.e., the die statement) that I used in my last posting and report back with the complete error message.


MDTech.us_MAN
Novice

Oct 14, 2012, 11:44 AM

Post #19 of 45 (12525 views)
Re: [FishMonger] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post


In Reply To
Your connect statement doesn't include any error handling. Add the error handling (i.e., the die statement) that I used in my last posting and report back with the complete error message.


Hello,

Here is the new error message (I think it's actually the same):

Code
Software error: 

DBI connect('bcpadb;host=instance28854.db.xeround.com:18847','maxwell175',...) failed: Can't connect to MySQL server on 'instance28854.db.xeround.com' (111) at /home/www/mdtech.us.ms/BCPA/BCPA_form/PROC/index.cgi line 16

For help, please send mail to the webmaster ([[no address given]]), giving this error message and the time and date of the error.

(I manually added the 2nd ] and [ )

Full code:
http://pastebin.com/fGhbHZKZ

Maxwell.


FishMonger
Veteran / Moderator

Oct 14, 2012, 2:03 PM

Post #20 of 45 (12515 views)
Re: [MDTech.us_MAN] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post

The verbiage of the error message you posted doesn't match the die statement in the code you posted, which tells me that the code you ran was not the code you posted or you altered the veriage when posting, so at this point it's anyone's guess as to the problem.


(This post was edited by FishMonger on Oct 14, 2012, 2:07 PM)


MDTech.us_MAN
Novice

Oct 18, 2012, 2:04 PM

Post #21 of 45 (12382 views)
Re: [FishMonger] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post


In Reply To
The verbiage of the error message you posted doesn't match the die statement in the code you posted, which tells me that the code you ran was not the code you posted or you altered the veriage when posting, so at this point it's anyone's guess as to the problem.


See for your self: http://mdtech.us.ms/BCPA/BCPA_form/


MDTech.us_MAN
Novice

Oct 18, 2012, 2:12 PM

Post #22 of 45 (12380 views)
Re: [MDTech.us_MAN] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post

I changed the code a little:
see this:
http://pastebin.com/VMQGzYSE

Maxwell.


FishMonger
Veteran / Moderator

Oct 18, 2012, 2:18 PM

Post #23 of 45 (12379 views)
Re: [MDTech.us_MAN] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post

You did not add the die statement as I asked you to do when connecting to the database. Please do so and report back with that error message.

Are you able to connect to the database from the command line?

Quote
mysql -h instance28854.db.xeround.com -P 18847 -u maxwell175 -p



FishMonger
Veteran / Moderator

Oct 18, 2012, 2:20 PM

Post #24 of 45 (12378 views)
Re: [MDTech.us_MAN] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post


In Reply To
I changed the code a little:
see this:
http://pastebin.com/VMQGzYSE

Maxwell.


You need to add $DBI::errstr to the die statement.


MDTech.us_MAN
Novice

Oct 20, 2012, 5:51 PM

Post #25 of 45 (12321 views)
Re: [FishMonger] Need help writing a script that interacts with a MySQL DB [In reply to] Can't Post


In Reply To

In Reply To
I changed the code a little:
see this:
http://pastebin.com/VMQGzYSE

Maxwell.


You need to add $DBI::errstr to the die statement.


Ok, I am VERY sorry. Here is the actual error message:

Code
Software error: 

Could not connect to the databaseCan't connect to MySQL server on 'instance28854.db.xeround.com' (111) at /home/www/mdtech.us.ms/BCPA/BCPA_form/PROC/index.cgi line 16.

For help, please send mail to the webmaster ([no address given]), giving this error message and the time and date of the error.


First page Previous page 1 2 Next page Last page  View All
 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives