in reply to Re: Modifying Perl script to write to MySQL??
in thread Modifying Perl script to write to MySQL??

Thanks so much CountZero! :-)

That looks very much like what I thought might do it.
I tried inserting your code (after the $guestbookurl line) but unfortunately something is returning a "500 Internal Server Error" ...

So here's the script (obviously there's something I'm not seeing that's causing the snag...):
#!/usr/bin/perl use DBI; # Guestbook Version 2.3.1 + # # Copyright 1996 Matt Wright mattw@worldwidemart.com + # # Created 4/21/95 Last Modified 10/29/95 + # # Scripts Archive at: http://www.worldwidemart.com/scripts/ + # # COPYRIGHT NOTICE + # # Copyright 1996 Matthew M. Wright All Rights Reserved. + # # + # # Guestbook may be used and modified free of charge by anyone so long +as # # this copyright notice and the comments above remain intact. By usin +g this # # code you agree to indemnify Matthew M. Wright from any liability tha +t # # might arise from it's use. + # # + # # Selling the code for this program without prior written consent is + # # expressly forbidden. In other words, please ask first before you tr +y and # # make money off of my program. + # # + # # Obtain permission before redistributing this software over the Inter +net or # # in any other medium. In all cases copyright and header must remain +intact.# # Set Variables $dbh = DBI->connect ("DBI:mysql:host=localhost;database=my_db", "my_id", "my_pass", {PrintError => 0, RaiseError => 1}); $guestbookurl = "http://www.usedflutes.com/confirmation.html"; $sth = $dbh->prepare($guestbookreal); $sth->execute(); $guestlog = "/home/yourname/public_html/guestlog.html"; $cgiurl = "http://64.177.252.244/home/flute/usedflutes-www/cgi-bin/gue +stbook.pl "; $date_command = "/bin/date"; # Set Your Options: $mail = 1; # 1 = Yes; 0 = No $uselog = 0; # 1 = Yes; 0 = No $linkmail = 1; # 1 = Yes; 0 = No $separator = 1; # 1 = <hr>; 0 = <p> $redirection = 1; # 1 = Yes; 0 = No $entry_order = 1; # 1 = Newest entries added first; # 0 = Newest Entries added last. $remote_mail = 1; # 1 = Yes; 0 = No $allow_html = 0; # 1 = Yes; 0 = No $line_breaks = 0; # 1 = Yes; 0 = No # If you answered 1 to $mail or $remote_mail you will need to fill out + # these variables below: $mailprog = '/usr/lib/sendmail'; $recipient = 'listings@usedflutes.com'; $updates = 'updates@usedflutes.com'; # Done # Get the Date for Entry $date = `$date_command +"%A, %B %d, %Y "`; chop($date); $shortdate = `$date_command +"%D %T %Z"`; chop($shortdate); # Get the input read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); # Split the name-value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); # Un-Webify plus signs and %-encoding $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s/<!--(.|\n)*-->//g; unless ($name eq 'username' or $name eq 'url') { $value = ucfirst($value); } if ($allow_html != 1) { $value =~ s/<([^>]|\n)*>//g; } if($name eq 'username') { $FORM{$name} = $value; } else { $FORM{$name} = $dbh->quote($value); } } # Print the Blank Response Subroutines &no_comments unless $FORM{'comments'}; &no_name unless $FORM{'realname'}; # chop comments field down to max if necessary $FORM{'comments'} = substr($FORM{'comments'},0,640) unless (length($FO +RM{'comments'}) <= 640); # URL biz $FORM{'url'} = "''" if $FORM{'url'} eq "'http://'"; # Begin the Editing of the Guestbook File open (FILE,"$guestbookreal") || die "Can't Open $guestbookreal: $!\n"; @LINES=<FILE>; close(FILE); $SIZE=@LINES; # Open Link File to Output open (GUEST,">$guestbookreal") || die "Can't Open $guestbookreal: $!\n +"; for ($i=0;$i<=$SIZE;$i++) { $_=$LINES[$i]; if (/<!--begin-->/) { if ($entry_order eq '1') { } { print GUEST "INSERT INTO `ads_2004` (`Title`, `Email`, `City` +, `State`, `Country`, `URL`, `Date`, `Description`, `rid`, `dt_create +`, `publish` ) VALUES ("; } if ($line_breaks == 1) { $FORM{'realname'} =~ s/\cM\n/\n/g; } print GUEST "$FORM{'realname'}"; print GUEST ", "; if ( $FORM{'username'} ){ my $email_name = $dbh->quote($FORM{'username'}); # if ($linkmail eq '1') { print GUEST $email_name; # } # else { # print GUEST "$FORM{'username'}"; # } print GUEST ", "; } if ( $FORM{'city'} ){ print GUEST "$FORM{'city'}"; } { print GUEST ", "; } if ( $FORM{'state'} ){ print GUEST "$FORM{'state'}"; } { print GUEST ", "; } if ( $FORM{'country'} ){ print GUEST "$FORM{'country'}"; } { print GUEST ", "; } if ($FORM{'url'}) { print GUEST "$FORM{'url'}"; } else { print GUEST "$FORM{'url'}"; } { print GUEST ", "; } if ($separator eq '1') { print GUEST "'$date'"; } else { print GUEST "'$date'"; } { print GUEST ", "; } if ( $FORM{'comments'} ){ print GUEST "$FORM{'comments'}, '', 'NOW()', 0) \n\n"; } if ($entry_order eq '0') { print GUEST "<!--begin-->\n"; } } else { print GUEST $_; } } close (GUEST); $dbh->disconnect; # Log The Entry
(end excerpt)

anyhow...

Thanks once again for your help with this,

-Bob

Replies are listed 'Best First'.
Re: Re: Re: Modifying Perl script to write to MySQL??
by davorg (Chancellor) on Feb 15, 2004 at 08:15 UTC
    Please don't use programs from Matt's Script Archive. They really aren't very good and you'll learn nothing but bad practices from reading that code. The guestbook at nms is a drop-in replacement for Matt's that is written in something approaching decent Perl :)
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Re: Re: Modifying Perl script to write to MySQL??
by CountZero (Bishop) on Feb 15, 2004 at 16:25 UTC
    I think you have inserted the code I proposed too early. At that place in the script the SQL-statement is not yet written and moreover $guestbookreal just holds the name of the file where the SQL-statement will end up.

    So you will have to retrieve the contents of that file (you can do that between the

    close (GUEST); $dbh->disconnect;

    statements and then plug it into a dbh->prepare statement there. You can then execute the SQL and put the data in your database.

    Why Matt goes through so many hoops to do something which is rather easy to do, no-one at Perlmonks has ever been able to understand.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      Hi CountZero,

      > moreover $guestbookreal just holds the name of the file where the SQL-statement will end up.

      But isn't that where the finished INSERT string/statement outputs to??
      $guestbookreal = "/home/flute/usedflutes-www/new_listings_publish.html +";
      http://usedflutes.com/new_listings_publish.html

      sorry, I'm somehow not getting the picture...

      Thanks again for your help CountZero,

      -Bob
      (cont.)

      Perhaps what I'm asking is if there is a way to execute the output string going to/thru $guestbookreal to MySQL "on-the-fly?"

      $guestbookreal=>mysql??

        $guestbookreal just holds the path and name of the file where your script will store the SQL-string you need to execute later: it does not hold this SQL-string itself directly.

        So what you need to do is to open this file and read it, put its contents in a scalar variable and execute that.

        I know, I know, it seems awfully convoluted and difficult and there is for sure a much more easy other solution available, but that would involve a major rewrite of your script. Then again, any rewrite of one of Matt's script is probably a "good thing™".

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law