
7stud
Enthusiast
Mar 25, 2010, 4:22 AM
Post #2 of 22
(16210 views)
|
Re: [demon01] send to a db and then to a script
[In reply to]
|
Can't Post
|
|
cgi script:
#!/usr/bin/env perl use strict; use warnings; use 5.010; use CGI ; use CGI::Carp qw{fatalsToBrowser warningsToBrowser}; #now *fatal* errors will automatically appear in the browser, #which is useful for debugging use DBI; my $query = new CGI; #for retrieving form data my $college = $query->param("college"); my $data_source = 'DBI:mysql:test'; my $user = 'root'; my $pass = ''; #dbh stands for 'db handle' my $dbh = DBI->connect( $data_source, $user, $pass, {RaiseError => 1, AutoCommit => 0}, #1=>true, 0=>false ); #sth stands for 'statement handle' my $sth = $dbh->prepare( "INSERT INTO college_counts(college, count) VALUES (?,?)" ); $sth->execute($college, 1); $dbh->commit(); #Because AutoCommit is false above $dbh->disconnect(); #Open a pipe to another script. Anything you #write to the filehandle will go to the other script's STDIN. open my $PIPE_WRITER, '| ../helper_programs/myprog.pl' or die "Couldn't open pipe to myprog.pl: $!"; say $PIPE_WRITER $college; close $PIPE_WRITER; my $query = new CGI; #for output back to browser print $query->header; warningsToBrowser(1); #Inserts warnings as html comments in the html source. #Can only be called after printing a header. print $query->start_html, $query->div("Your college choice, $college, was entered into our db."), $query->div("$college was also sent to another script"), $query->end_html ; Another script:
#!/usr/bin/env perl use strict; use warnings; use 5.010; #On my server, the relative path from cgi-bin to here is: #../helper_programs/myprog.pl #The path will most likely be different on your server. #Open a file and write the received data to the file to prove #that this program received the data: open my $OUTFILE, '>', '../writables/data1.txt' or die "Couldn't open file data1.txt: $!"; while (<STDIN>) { #this program expects line oriented input, i.e. lines that #end with "\n". When the other program closes the pipe #this while() loop gets an eof signal print $OUTFILE $_; #do whatever needs to be done with the data } close $OUTFILE; Make sure both scripts have read+execute permissions, and proper shebang lines at the top.
(This post was edited by 7stud on Mar 25, 2010, 5:07 AM)
|