bobafifi has asked for the wisdom of the Perl Monks concerning the following question:
close (GUEST); 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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Right answer (wrong question...)
by jeffa (Bishop) on Feb 22, 2004 at 18:09 UTC | |
And use placeholders (those question marks). Now, if your form fields are named JUST LIKE your database table columns, then you can do some tricks to save typing and prevent typos: By the way, your SQL snippet has ELEVEN fields and only TEN values. This is the kind of typo i was talking about. ;) jeffa L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat) | [reply] [d/l] [select] |
by bobafifi (Beadle) on Feb 22, 2004 at 23:39 UTC | |
Your code looks great but unfortunately, I'm not quite sure how to integrate it into the existing script I use, sorry. However, I've been creating a new version of the script with all the field names matching the database as you suggested but am not quite there yet. Thanks again, :-) -Bob "so many drummers, so little time" | [reply] |
|
Re: Right answer (wrong question...)
by jarich (Curate) on Feb 23, 2004 at 01:37 UTC | |
I whole-heartedly agree with davorg's advice that you replace your original script with the drop in replacement from nms. This code is free, well written and more secure than Matt's scripts. It is probably easier to improve in the manner you're trying than Matt's scripts will be, too. Anyway, I hope this now makes sense to you. jarich | [reply] [d/l] |
by bobafifi (Beadle) on Feb 23, 2004 at 01:55 UTC | |
The script already produces INSERT strings that work fine. Count Zero's code does excatly what I'm after, that is, it posts an INSERT string into MySQL. The problem is, that string is from an example that I made and placed on an HTML page, not what the form generates. In other words, every time the form is submitted, the same INSERT string posts. What I'm hoping somebody can see here is how to get the INSERT strings from the form to post - either by modifying CountZero's code or with something else - rather than the sample INSERT. Thanks again, | [reply] |
|
An exercise in mind reading
by jarich (Curate) on Feb 23, 2004 at 07:36 UTC | |
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:
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. Great, you connect to the database. You almost certainly want to add ShowErrorStatement => 1 in next to RaiseError. Code ugliness aside, what's happening here is that we're saving the current contents of $guestbookreal into @LINES. 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. 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. 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: to with: 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 | [reply] [d/l] [select] |
by bobafifi (Beadle) on Feb 24, 2004 at 18:44 UTC | |
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 | [reply] |
by bobafifi (Beadle) on Feb 24, 2004 at 19:35 UTC | |
delete and/or modify these?? (from the top of the script and) (from the MySQL section) ===== Thanks, | [reply] [d/l] [select] |
|
Re: Right answer (wrong question...)
by graff (Chancellor) on Feb 22, 2004 at 23:12 UTC | |
| [reply] [d/l] |
by bobafifi (Beadle) on Feb 22, 2004 at 23:31 UTC | |
Thanks! -Bob | [reply] |
|
Re: Right answer (wrong question...)
by MCS (Monk) on Feb 23, 2004 at 03:43 UTC | |
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. | [reply] |
by bobafifi (Beadle) on Feb 23, 2004 at 04:57 UTC | |
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, | [reply] [d/l] |
by MCS (Monk) on Feb 23, 2004 at 16:09 UTC | |
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:
Would become:
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: 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. | [reply] [d/l] [select] |
by bobafifi (Beadle) on Feb 24, 2004 at 22:15 UTC | |
by jarich (Curate) on Feb 24, 2004 at 23:07 UTC | |
| |