Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

An exercise in mind reading

by jarich (Curate)
on Feb 23, 2004 at 07:36 UTC ( [id://331047]=note: print w/replies, xml ) Need Help??


in reply to Right answer (wrong question...)

G'day bobafifi,

What you're really asking of us is for us to read your mind. There's a whole heap of information that you should have put into this question. This includes the following:

  • how much experience you've had with Perl
  • what you've tried
  • what you think is happening
  • what relation the snippet of code above has to your problem
  • where/when the file in $guestbookreal gets written.

You can read more about what kind of information we like to see in questions over at our tutorials page.

You've written this question assuming that we've already read the previous discussion from last week. If this question is entirely dependant on that discussion then this node should have gone into there, not created a new question. If this question is a new question then you need to write out all the relevant information (and only the relevant information) so that people like me can see everything I need here.

Now that I've read last week's discussion I'll discuss what's happening in your code over here.

$dbh = DBI->connect ("DBI:mysql:host=localhost;database=my_db", "my_id", "my_pass", {PrintError => 0, RaiseError => 1});
Great, you connect to the database. You almost certainly want to add ShowErrorStatement => 1 in next to RaiseError.
# Begin the Editing of the Guestbook File open (FILE,"$guestbookreal") || die "Can't Open $guestbookreal: $!\n"; @LINES=<FILE>; close(FILE); $SIZE=@LINES;
Code ugliness aside, what's happening here is that we're saving the current contents of $guestbookreal into @LINES.
# Open Link File to Output open (GUEST,">$guestbookreal") || die "Can't Open $guestbookreal: $!\n +";
Now we're deleting everything from $guestbookreal and opening it for writing. This is just asking for 2 scripts to try to access the file at once, or any other of a myriad of problems.
for ($i=0;$i<=$SIZE;$i++) { $_=$LINES[$i]; if (/<!--begin-->/) { if ($entry_order eq '1') { } { print GUEST "INSERT INTO `mysql_db` (`Title`, `Email`, `City`, `State`, `Country`, `URL`, `Date`, `Description`, `rid`, `dt_create`, `publish` ) VALUES ("; } .... } else { print GUEST $_; } } close (GUEST);
This code is generating SQL for all lines in $guestbookreal that contain the string "<!--begin-->" in them. Lines without that string are printed back into the $guestbookreal as they were previously. Once they're all printed back into the file the file is closed.
my $content; open FILE, '<', $guestbookreal; # Open a filehandle FILE to $guestbook +real while (<FILE>) { chomp; # delete any EOL character(s) $content .= $_; # Add the next line to the previous parts } $sth=$dbh->prepare($content); $sth->execute(); $dbh->disconnect;
This code then reads all the entries out of $guestbookreal, prepares them into one huge sql statement and executes that. I have no idea what should happen here as presumably your file contains more than one sql statement and I've never tried to do that with prepare. Nevertheless, the end result of this script is going to be to add everything already in $guestbookreal into the database (probably "again").

Because you've cut so much out of the main for loop I have no idea what specifically is wrong here. It appears obvious that the script is supposed to add your new entry (submitted from the guestbook form) into the $guestbookreal file as the first line. You seem to have lost the lines that would do that.

You're trying to shoehorn a database onto code which is designed to store guestbook entries into a flat HTML file. I recommend that instead of generating your SQL and printing it to a file you consider rewriting this part of the script entirely. Perhaps you could replace everything from:

# Begin the Editing of the Guestbook File
to
$dbh->disconnect;
with:
# Check that the things which must have a value have values: if(defined $FORM{Description} and defined $FORM{Title}) { my $sql = "INSERT INTO `mysql_db` (`Title`, `Email`, `City`, `State`, `Country`, `URL`, `Date`, `Description`, `rid`, `dt_create`, `publish` ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; my $sth = $dbh->prepare($sql); $sth->execute($FORM{Title}, $FORM{Email}, $FORM{City}, $FORM{State}, $FORM{Country}, $FORM{URL}, $FORM{Date}, $FORM{Description}, $FORM{rid}, $FORM{dt_create}, $FORM{publish}); $dbh->disconnect; } else { print "You must enter a Description and a Title"; }
This at least would make sense.

Once more I'll recommend that you change to using nms scripts rather than Matt Wrights'. As you've experienced in the past, Matt's scripts have a few security issues, and the hand-rolled CGI parameter parsing in the script I discuss here, makes me cringe.

Good luck.

jarich

Replies are listed 'Best First'.
Re: Right answer (wrong question...)
by bobafifi (Beadle) on Feb 24, 2004 at 18:44 UTC
    Hi jarich,

    Thanks for your reply - I had no idea that Matt's script was doing all that deleting/rewriting biz - yikes!

    anyhow...

    I tried inserting your code as suggested but am getting "Server Error" replies. While what you're suggesting does indeed make lots of sense, it seems that it also involves really overhauling most of the script - as well as the form - to reflect the new fields and their values, right?

    Since the script as it is ("code ugliness" etc., notwithstanding...), already spits out working INSERT strings (with the field values for 'rid', 'dt_create', and 'publish' all included in the "Description" field - I know this does not reflect how many fields are in the form, but since the string output matches the database fields, it "works"...), I guess I don't understand why that same data can't somehow be redirected to MySQL along the lines of CountZero's code which does successfully post INSERT strings to MySQL - just from the wrong location?

    Also, since Matt's <!begin> biz is not needed for inserting into MySQL and screws things up if it's there (which is why I removed it), I'm wondering if perhaps there's a way to simply delete/replace/avoid/modify those parts of the script, yet still process the form? If so, shouldn't it then be possible to execute the strings as they output?

    Thanks again jarich,

    -Bob

    bobafifi.com
Re: Right answer (wrong question...)
by bobafifi (Beadle) on Feb 24, 2004 at 19:35 UTC
    (cont.)

    delete and/or modify these??
    $mysql = "/home/flute/usedflutes-www/new_listings_mysql.html";
    (from the top of the script and)
    # Begin the Editing of the MySQL File open (FILE,"$mysql") || die "Can't Open $mysql: $!\n"; @LINES=<FILE>; close(FILE); $SIZE=@LINES; # Open Link File to Output open (MYSQL,">$mysql") || die "Can't Open $mysql: $!\n"; for ($i=0;$i<=$SIZE;$i++) { $_=$LINES[$i]; if (/<!--begin-->/) {
    (from the MySQL section)
    # MySQL # Begin the Editing of the MySQL File open (FILE,"$mysql") || die "Can't Open $mysql: $!\n"; @LINES=<FILE>; close(FILE); $SIZE=@LINES; # Open Link File to Output open (MYSQL,">$mysql") || die "Can't Open $mysql: $!\n"; for ($i=0;$i<=$SIZE;$i++) { $_=$LINES[$i]; if (/<!--begin-->/) { if ($entry_order eq '1') { } { print MYSQL "INSERT INTO `mysql_db` (`Title`, `Email`, `City` +, `State`, `Country`, `URL`, `Date`, `Description`, `rid`, `dt_create +`, `publish`, ) VALUES ("; } if ($line_breaks == 1) { $FORM{'Title'} =~ s/\cM\n/\n/g; } print MYSQL "$FORM{'Title'}"; print MYSQL ", "; if ( $FORM{'Email'} ){ my $email_name = $dbh->quote($FORM{'Email'}); # if ($linkmail eq '1') { print MYSQL $email_name; # } # else { # print MYSQL "$FORM{'Email'}"; # } print MYSQL ", "; } if ( $FORM{'City'} ){ print MYSQL "$FORM{'City'}"; } { print MYSQL ", "; } if ( $FORM{'State'} ){ print MYSQL "$FORM{'State'}"; } { print MYSQL ", "; } if ( $FORM{'Country'} ){ print MYSQL "$FORM{'Country'}"; } { print MYSQL ", "; } if ($FORM{'URL'}) { print MYSQL "$FORM{'URL'}"; } else { print MYSQL "$FORM{'URL'}"; } { print MYSQL ", "; } if ($separator eq '1') { print MYSQL "'$date'"; } else { print MYSQL "'$date'"; } { print MYSQL ", "; } if ( $FORM{'Description'} ){ print MYSQL "$FORM{'Description'}, '', NOW(), 0) \n\n"; } if ($entry_order eq '0') { print MYSQL "<!--begin-->\n"; } } else { print MYSQL $_; } } close (MYSQL); my $content; open FILE, '<', $mysql; # Open a filehandle FILE to $mysql while (<FILE>) { chomp; # delete any EOL character(s) $content .= $_; # Add the next line to the previous parts } $sth=$dbh->prepare($content); $sth->execute(); close (MYSQL); $dbh->disconnect;
    =====

    Thanks,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://331047]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-18 06:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found