Online insert MySQL rows by perl-script

 
Thread Tools Search this Thread
Special Forums UNIX and Linux Applications Online insert MySQL rows by perl-script
# 1  
Old 01-04-2012
Online insert MySQL rows by perl-script

Hello,

Met a problem when I tried to insert rows to MySQL database from an old book that fits my learning level (MySQL and Perl for the Web, by Paul DuBois, 2001). First, under mysql console I created a database: webdb and the table: todo. Then I draft the perl-cgi script to have online page. The purpose is to input data to my database through webpage, as learning. Now the problem is: everything seems running, but the table is not updated as I insert data through online form. Please give advice and I want to make progress of this self-learning. Thank you very much!
Here is my code:
Code:
#!/usr/bin/perl -w
#todo1.pl  --- initial version of the to-do application

use strict;
use lib qw(/usr/lib/perl5);
use CGI qw(:standard);
use WebDB;

print header(),
     start_html(-title=>"To-Do List", -bgcolor=>"white"),
     h2("To-Do List");

#Dispatch to proper action based on user selection

my $choice = lc (param("choice"));  #get choice, lowercased

if ($choice eq "") {         #initial script invocation

     display_entry_form();

} 
elsif ($choice eq "submit") {

     insert_item(param("content"));
     display_entry_form();

}
else {

     print p("Logic error, unknown choice: $choice");

}

print end_html();


sub insert_item {

my $content = shift;
my $dbh;
    $content =~ s/^\s+//;        #strip leading whitespace
    $content =~ s/^\s+$//;        #strip trailing whitespace
    if ($content ne "") {         #if content is non-empty, add to table
        $dbh = WebDB::content();
        $dbh->do(qq{
           INSERT INTO todo SET t = NOW(), status = 'open', content = ?},
       undef, $content);

         $dbh->disconnect();

    }
}

sub display_entry_form {
    print start_html(-action=>url()),
    "To-do item:", 
        br(),
    textarea(-name =>"content",
         -value => "",
         -override => 1,
         -rows => 3,
         -columns => 80),
    br(),
    submit(-name => "choice", -value => "Submit"),
    end_form();
}

###########END OF MAIN ################

Code:
########    WebDB    ##################

#which is located in /usr/lib/perl5/

package WebDB;
#package WebDB for later use;

use strict;
use DBI;

my $db_name="webdb";
my $user_name="webdev";
my $password="secretpass";
my $host_name="localhost";
my $dsn = "DBI:mysql:host=$host_name; database=$db_name";

#connect to MySQL server, using hardwired name and password

sub connect 
{
    return (DBI->connect($dsn, $user_name, $password,
        {PrintError=>0, RaiseError=>1})) 
        || die "Could not connect to database: $DBI::errstr";
}

#Connect to MySQL server, using name and password from the current 
#user's  ~/.my.conf option file. The mysql_read_default_file option,
#when added to the DSN, specifies which option file to read.

sub connect_with_option_file 
{
    $dsn .= "; mysql_read_default_file=$ENV{HOME}/.my.conf";
    return (DBI->connect ($dsn, $user_name, $password, {PrintError=>0, RaiseError=>1}));
}

1;

The interesting thing is I got the online form, but after I submitted some data, say:
Code:
Finish chapter 6 before the editor called me
Fix chapter 5 code examples
Revise chapter 10 outline

I am supposed to get webdb updated by:
Code:
mysql> SELECT * FROM todo;
Empty set (0.00 sec)

but nothing shows up. Can anybody give some advice? Thanks a lot!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk online to exclude rows

Hello, I have 3 columns like shown below: 1 1800 1900 2 1765 1900 3 1654 2054 4 1326 1499 5 1540 1765 I want only those rows where column 2 and column 3's values don't fall within 1800-1900 both inclusive. My output should only be: 4 1326 1499 5 1540 1765 Is there a quick awk... (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

2. Shell Programming and Scripting

Need help on Insert data to phpMyAdmin mySQL database from Shell Script

Sorry to disturb you, I would like to seek help on inserting data whenever the switch is on or off to my phpMyAdmin mySQL database from my Shell Script. I'm using Raspberry PI as my hardware and I have follow this LINK: instructables.com/id/Web-Control-of-Raspberry-Pi-GPIO/?ALLSTEPS to create my... (4 Replies)
Discussion started by: aoiregion
4 Replies

3. Shell Programming and Scripting

Convert columns to rows in perl script

Hi All I want to have a Perl script which convert columns to rows. The Perl should should read the data from input file. Suppose the input file is 7215484 date to date 173.3 A 1.50 2.23 8.45 10.14 2.00 4.50 2.50 31.32 7216154 month to month (3 Replies)
Discussion started by: parthmittal2007
3 Replies

4. Programming

Help with mySQL database by perl script

Hello; I was trying to set up a mysql database using following script, but never went through. The code seems fine without any syntax error as I tested it: perl -c Brapa0101-db.pl Brapa0101-db.pl syntax OKHowever, whenever I run it, an error message was tossed out: DBD::mysql::st execute... (7 Replies)
Discussion started by: yifangt
7 Replies

5. Shell Programming and Scripting

Help with perl mysql backup script

Hi, im trying to make a script that backups mysql databases but apparently I am having trouble with the variables, or simply something I am missing. Would appreciate any help, here is the script #!/usr/bin/perl -w use strict; require File::Spec; #VARIABLES my $databasename =... (4 Replies)
Discussion started by: Fireline
4 Replies

6. Shell Programming and Scripting

how to print out data from mysql table in perl script

I'm having trouble with this code. if i do .\read.pl -u user it prints out 2010-12-20 12:00:00 host1 <cmd>a 2010-12-20 12:00:01 host1 <cmd> <execute> 2010-12-20 12:00:02 host1 <cmd>b 2010-12-20 12:00:03 host1 <cmd>c however, if i enter .\read.pl -h host1 it should... (3 Replies)
Discussion started by: kpddong
3 Replies

7. Shell Programming and Scripting

Please do help: Perl Script to pull out rows from a CSV file

I have CSV file that contains data in the format as shown below: ABC, 67, 56, 67, 78, 89, 76, 55 PDR, 85, 83, 83, 72, 82, 89, 83 MPG, 86, 53, 54, 65, 23, 54, 75 .. .. .. .. I want to create a script that will pull out the rows from the above sheet and paste it into another CSV file.... (12 Replies)
Discussion started by: pankajusc
12 Replies

8. Shell Programming and Scripting

shell script to insert data from gps.txt to mysql database

Hi, I have gps receiver, by using gpsd data i can read gps log data to my database(my sql). Steps: 1. telenet localhost 2947 > gps.txt (press enter) 2. r (press enter) //then i will get the data like below in gps.txt file Trying 127.0.0.1... Connected to localhost.... (1 Reply)
Discussion started by: gudivada213
1 Replies

9. Shell Programming and Scripting

perl script to print to values in rows and columns

Hi guys I want to print the values by using this script but its giving the no of rows and columns as input instead of values Would you plz help me on this FILE- chr1.txt 1981 1 1971 1 1961 1 1941 1 perl script #!/usr/bin/perl -w $infile1 = 'chr1.txt'; $outfile3 = 'out3.txt'; ... (3 Replies)
Discussion started by: nogu0001
3 Replies

10. Shell Programming and Scripting

Connecting MySql throug Perl Script ?

Dear Friends, I am tryin to connect to the myql through perl scrip. I have already installed mysql and DBI modules to my Perl. There versions are as follows, DBD-mysql MySQL driver for the Perl5 Database DBI Database independent interface for It gives... (4 Replies)
Discussion started by: maheshsri
4 Replies
Login or Register to Ask a Question