From ccd3c6ab07fe9e728fd614544c7835e511320e37 Mon Sep 17 00:00:00 2001 From: josch Date: Thu, 11 Oct 2007 10:00:38 +0000 Subject: [PATCH] cleaned up mysql statements git-svn-id: http://yolanda.mister-muffin.de/svn@20 7eef14d0-6ed0-489d-bf55-20463b2d70db --- trunk/README_DATABASE | 1 - trunk/functions.pl | 24 ++++++++++++------------ trunk/include.pl | 1 - trunk/register.pl | 16 +++++----------- trunk/uploader.pl | 14 ++++++++++++++ 5 files changed, 31 insertions(+), 25 deletions(-) diff --git a/trunk/README_DATABASE b/trunk/README_DATABASE index 560b75b..9a4de54 100644 --- a/trunk/README_DATABASE +++ b/trunk/README_DATABASE @@ -8,5 +8,4 @@ fill with some data: eg.: insert into tagcloud values ('web tv', 68); create table users (id int auto_increment not null, username varchar(255) not null, password char(41) not null, primary key (id)); - insert into users (username, password) values ('user', password('pass')); diff --git a/trunk/functions.pl b/trunk/functions.pl index 61c7be3..7f6f200 100644 --- a/trunk/functions.pl +++ b/trunk/functions.pl @@ -3,13 +3,13 @@ require "/var/www/perl/include.pl"; #get tags from database and fill $page with xml sub fill_tagcloud { #connect to db - $dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass); + my $dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass) or die $dbh->errstr; #prepare query - my $sth = $dbh->prepare(qq{select text, count from tagcloud }); + my $sth = $dbh->prepare(qq{select text, count from tagcloud }) or die $dbh->errstr; #execute it - $sth->execute(); + $sth->execute() or die $dbh->errstr; #get every returned value while (my ($text, $count) = $sth->fetchrow_array()) @@ -19,10 +19,10 @@ sub fill_tagcloud { } #finish query - $sth->finish(); + $sth->finish() or die $dbh->errstr; #close db - $dbh->disconnect(); + $dbh->disconnect() or die $dbh->errstr; } #return a username from passed session id @@ -31,23 +31,23 @@ sub get_username_from_sid { my ($sid) = @_; #connect to db - $dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass); + my $dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass) or die $dbh->errstr; #prepare query - my $sth = $dbh->prepare(qq{select username from users where sid = '$sid'}); + my $sth = $dbh->prepare(qq{select username from users where sid = '$sid'}) or die $dbh->errstr; #execute it - $sth->execute(); + $sth->execute() or die $dbh->errstr; #save the resulting username - my ($username) = $sth->fetchrow_array(); + my ($username) = $sth->fetchrow_array() or die $dbh->errstr; #finish query - $sth->finish(); + $sth->finish() or die $dbh->errstr; #close db - $dbh->disconnect(); + $dbh->disconnect() or die $dbh->errstr; - #return username + #return return $username; } diff --git a/trunk/include.pl b/trunk/include.pl index c2cc9cb..b0a79bf 100644 --- a/trunk/include.pl +++ b/trunk/include.pl @@ -10,5 +10,4 @@ $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $session_name = 'sid'; -$cwd = $ENV{PWD}; 1; diff --git a/trunk/register.pl b/trunk/register.pl index 39ed023..09f660b 100644 --- a/trunk/register.pl +++ b/trunk/register.pl @@ -8,23 +8,17 @@ $session = new CGI::Session; #if username and password are passed put them into the database if($query->param('user') and $query->param('pass')) { #connect to db - $dbh = DBI->connect("DBI:mysql:$database:$host", $dbuser, $dbpass); + my $dbh = DBI->connect("DBI:mysql:$database:$host", $dbuser, $dbpass) or die $dbh->errstr; #save POST data in local variables my $user = $query->param("user"); my $pass = $query->param("pass"); - #prepare query - $sth = $dbh->prepare(qq{insert into users (username, password) values ('$user', password('$pass'))}); - - #execute query - $sth->execute(); - - #finish query - $sth->finish(); - + #do query + $dbh->do(qq{insert into users (username, password) values ('$user', password('$pass'))}) or die $dbh->errstr; + #disconnect db - $dbh->disconnect(); + $dbh->disconnect() or die $dbh->errstr; #print a little confirmation print $session->header(); diff --git a/trunk/uploader.pl b/trunk/uploader.pl index d5b6294..7e744e8 100644 --- a/trunk/uploader.pl +++ b/trunk/uploader.pl @@ -8,6 +8,7 @@ $session = new CGI::Session; sub hook { #this is going to become an ajax progress bar my ($filename, $buffer, $bytes_read, $data) = @_; + print $ENV{'CONTENT_LENGTH'}; print sha256_hex($buffer); #open(TEMP, ">>/var/www/perl/videos/temp.temp") or die "cannot open"; print "Read $bytes_read bytes of $filename\n"; @@ -17,6 +18,15 @@ sub hook { my $username = get_username_from_sid($session->id); if($username) { + #connect to db + my $dbh = DBI->connect("DBI:mysql:$database:$host", $dbuser, $dbpass) or die $dbh->errstr; + + #save POST data in local variables + my $title = $query->param("title"); + + #do query + $dbh->do(qq{insert into videos (title, username) values ('$title', '$username')}) or die $dbh->errstr; + my $filename = $query->param("file"); my $title = $query->param("title"); $upload_filehandle = $query->upload("file"); @@ -25,6 +35,10 @@ if($username) { { print; } + + + #disconnect db + $dbh->disconnect() or die $dbh->errstr; } else { print $session->header(); print "nope...";