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. |