David Silva wrote:
> Hi Matthew,
> 
> I resolved the problem like this :
> 
> # HTML::Mason
> 
> if ($validar){
>     my $dbcon= DBI->connect("dbi:mysql:dbname=dbcontactos","root","");
>     my $sql = $dbcon->prepare("select name, pass from Utilizador where
> name=? and pass=?");
>         $sql->execute($name,$pass) or die "erro" . $sql->errstr;
> 
>     if($sql->rows == 1){ # muda de página
>         $m->redirect('main.html');
>         # $token = 1; #send token to main.html but not yet
> 
>     }
>     else{ # continua na mesma página
>         $m->redirect('login.html');
>         # $token = 0;
>     }
> }
> 
> Thank you for your reply!
> 
> 2009/10/14 Matthew Ramadanovic <matthew.ramadano...@yale.edu>
> 
>>
>>> my $dbcon= DBI->connect("dbi:mysql:dbname=dbcontactos","root","");
>>> my $sql = $dbcon->prepare("select name,pass from Utilizador where
>> name='?' >and pass='?'");
>>> $sql->execute($user,$pass);
>>>    if($sql == 1){
>>> redirect to other page
>>>    }
>>>    if($sql == 0){
>>> stay in the login page
>>>    }
>> Not quite sure what you are expecting $sql to be. If your intention is
>> to compare $pass or other table variables to the value in column before
>> proceeding why not just pull it into a structure that will allow you to
>> do so? :
>>
>> my $pass;
>> my $name;
>> my %record;
>> my $sql = "select name,pass from Utilizador where name='?' and
>> pass='?'";
>> my $sth = $dbh->prepare($sql);
>> $sth->execute($name,$pass);
>> my $rowref;
>> my $column;
>> my $val;
>> while ($rowref = $sth->fetchrow_hashref()) {
>>        #not sure if you will have multiple records so you may need to
>> exit
>>        #the loop after %record is filled the first time
>>        while (($column, $val) = each %$rowref) {
>>                $record{$column} = $val;
>>        }
>> }
>> If ($record{'pass'} eq $pass) {
>>      #redirect to other page
>> } else {
>>        #stay on logon
>> }
>>
>>
>> -M
>>
>>
>> -----Original Message-----
>> From: David Silva [mailto:david...@gmail.com]
>> Sent: Wednesday, October 14, 2009 6:30 AM
>> To: dbi-users@perl.org
>> Subject: Stuck with form validation
>>
>> Hi everyone,
>>
>> I have a form with username and password, and when the user enter the
>> right
>> user and pass he/she go to other page. (that is my thought)
>>
>> What i did?
>>
>>    my $dbcon= DBI->connect("dbi:mysql:dbname=dbcontactos","root","");
>>    my $sql = $dbcon->prepare("select name,pass from Utilizador where
>> name='?' and pass='?'");
>>        $sql->execute($user,$pass);
>>    if($sql == 1){
>> redirect to other page
>>    }
>>    if($sql == 0){
>> stay in the login page
>>    }
>>
>> My problem is that i can't see any other way to do this and even if the
>> values are correct with the ones in database it stays in the login page.
>>
>> How can i do this?
>>
>> Thank you
>> --
>> David Silva
>>
> 

You may need to be aware that call to rows might not reliable across all
DBDs. rows is documented as:



Returns the number of rows affected by the last row affecting command,
or -1 if the number of rows is not known or not available.

Generally, you can only rely on a row count after a non-SELECT execute
(for some specific operations like UPDATE and DELETE), or after fetching
all the rows of a SELECT statement.



Also, you issued a select and did not read the row or close the cursor
(although I believe once "$sql" goes out of scope the latter does not
matter). If username is a unique then why don't you just do:

my $sql = $dbcon->prepare(q{select 1 from Utilizador where name=? and
pass=?});
$sql->execute($user,$pass);
my $found = $sql->fetch;
if ($found) {
  redirect to other page
} else {
  stay in the login page
}

Martin
-- 
Martin J. Evans
Easysoft Limited
http://www.easysoft.com

Reply via email to