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

Ok, I'm not quite sure what you are after but it sounds like instead of reading text from a file, you want to read it from a form. Is that right?

If so, you need to look at something like cgi.pm This will allow you to create a form, and then you can get the value of that form when you hit submit.

So basically, do you have the form created already? Is the action on that form your perl script? Is your perl script getting the values? If you answered yes, then you just need to submit that to your database. If you answered no to any of those questions, you need to give us more information and code. Suggested snippets of code to include: Part of the form that shows a field you want to insert, your sql insert statement and anything else you think might help.

Replies are listed 'Best First'.
Re: Re: Right answer (wrong question...)
by bobafifi (Beadle) on Feb 23, 2004 at 04:57 UTC
    Hi MCS,

    OK - here's the situation:

    I posted this problem last week http://perlmonks.org/index.pl?node_id=329045
    (there you can see my original question, code examples and replies).

    During the back-and-forth of replies, CountZero provided something that worked - well, almost...

    The script generates INSERT strings whenever the form is submitted. Normally, these strings post to an HTML page ( as well as to an e-mail send that I get).
    By selecting, copying and pasting the INSERT strings into a GUI and hitting "GO" they are added to the MySQL and work fine.

    For some reason (which I do not understand), inserting CountZero's code somehow turns the POST function of the script

     $guestbookreal = "/home/flute/usedflutes-www/new_listings_publish.html";

    to a GET function of the sample INSERT string sitting on the HTML page - not from the form.

    Since CountZero's code does post the INSERT string to MYSQL - but from the wrong location - I'm trying to find out what the *right* location is of the INSERT string as it outputs from the form - so that is what goes into MySQL.

    Thanks again,

      Wow... first, a major rewrite is in order. I looked over your code from the other post and I realize most of it is from Matt's script archive but I think even you, as a beginner, can write better code than that. That said I offer the following suggestions for your existing code:

      It appears you are getting the input from STDIN which doesn't make sense if it is from a form. but if it is working and all you want to do is automate putting the print statement into a database, instead of (or in addition to) all your lines that say print GUEST you would need a line that says something like $insert .= and then the rest of the line. Example:

      print GUEST "INSERT INTO `mysql_db` (`Title`, `Email`, `City`, `State` +, `Country`, `URL`, `Date`, `Description`, `rid`, `dt_create`, `publi +sh` ) VALUES ("; } if ($line_breaks == 1) { =~ s/\cM\n/\n/g; } print GUEST ""; print GUEST ", ";

      Would become:
      my $insert = "INSERT INTO `mysql_db` (`Title`, `Email`, `City`, `State +`, `Country`, `URL`, `Date`, `Description`, `rid`, `dt_create`, `publ +ish` ) VALUES ("; } if ($line_breaks == 1) { =~ s/\cM\n/\n/g; } $insert .= ""; # This can be removed as it doesn't do anythin +g $insert .= ", ";

      As you can see, first I initialized $insert which would be done the first time you need to write it and then after that I use the concatinate operator (.=) to add text to $insert. Then when you are done, you just need to change $sth=$dbh->prepare($content); to $sth=$dbh->prepare($insert); and then it should insert your code into the database.

      Some suggestions:
      I suggest you pick up learning perl (and maybe a few other good books, check the book review section of this site) and rewrite your code. Use CPAN and check out some of it's modules, the documentation is usually quite good. Specifically look at CGI.pm which you can use to create all of your forms and then use the data from said forms to populate the database. (put the information in it) Writing to a file and then reading it again to enter the data into a database is VERY slow and while that may not be a big issue to you now, it might later on. Also problems can arrise if 2 people try and write to your guestbook at the same time. These issues go away when you stop writing to disc all the time.

      So maybe get your current version working and then take it upon yourself to rewrite it once it's working. Trust me, it's worth it to write it yourself. With good resources like perl monks as well as the countless books and web tutorials on perl, even someone with no programming experience can learn, it just takes a little time.

        Thanks MCS,

        You mean like this? (getting errors, so maybe something simple here I'm messing up?)
        # MySQL if ($entry_order eq '1') { } { my $insert = "INSERT INTO `mysql_db` (`Title`, `Email`, `Cit +y`, `State`, `Country`, `URL`, `Date`, `Description`, `rid`, `dt_crea +te`, `publish`, ) VALUES ("; } if ($line_breaks == 1) { $FORM{'Title'} =~ s/\cM\n/\n/g; } $insert .= "$FORM{'Title'}"; $insert .= ", "; if ( $FORM{'Email'} ){ my $email_name = $dbh->quote($FORM{'Email'}); # if ($linkmail eq '1') { $insert .= $email_name; # } # else { # $insert .= "$FORM{'Email'}"; # } $insert .= ", "; } if ( $FORM{'City'} ){ $insert .= "$FORM{'City'}"; } { $insert .= ", "; } if ( $FORM{'State'} ){ $insert .= "$FORM{'State'}"; } { $insert .= ", "; } if ( $FORM{'Country'} ){ $insert .= "$FORM{'Country'}"; } { $insert .= ", "; } if ($FORM{'URL'}) { $insert .= "$FORM{'URL'}"; } else { $insert .= "$FORM{'URL'}"; } { $insert .= ", "; } if ($separator eq '1') { $insert .= "'$date'"; } else { $insert .= "'$date'"; } { $insert .= ", "; } if ( $FORM{'Description'} ){ $insert .= "$FORM{'Description'}, '', NOW(), 0) \n\n"; } if ($entry_order eq '0') { $insert .= "<!--begin-->\n"; } } else { $insert .= $_; } } $sth=$dbh->prepare($insert); $sth->execute(); $dbh->disconnect;
        Thanks,

        -Bob

        bobafifi.com