Perl舞者SQL问题

时间:2018-07-02 12:33:36

标签: sqlite perl dancer

我正在建立一个具有管理员登录页面,学生登录,公司登录和申请表的网站。

我正在使用Perl舞者框架。

申请表包含:

Name
Mobile_no
Gender 
Password
Email 
Address
SSLC
HSC
CGPA
Proficient languages
Internships
Preferred location

HTML的相应代码如下:

    <!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">

  <title> APPLICATION FORM </title>

<style>
*
{
    margin: 0;
    padding: 0;
}
.cont {
    background:url("/images/2.jpg");
    background-size: 100%;
    background-repeat: no-repeat;
    padding: 20px;
    height: 750px;
}
form 
{ 
    height: auto;
    border-radius:1px;
    margin: 1% auto;
    padding: 1%;
    background-color:darkgrey; 
    margin-left: 38%;
    width: 30%;
}
input[type=submit] {
    margin-left: 10%;
    background-color:#D14B57;
    color: white;
    padding: 12px 20px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}
input[type=submit]:hover {
  background-color:green;

}
</style>
</head>

<div class="cont">
  <form method="post">
 <h2 style="text-align: center; text-decoration:underline;" id="contactus">APPLICATION FORM </h2> <br>

   <label for="fname"><strong>Name*</strong></label>
    <input style="margin-left: 26%;" type="text" id="fname" name="name" placeholder="Your name..."> <br> <br>

    <label for="mno"><strong>Mobile Number*</strong></label>
    <input style="margin-left: 7%;" type="text" id="mno" name="mobile_no" placeholder="Your Mobile Number..."> <br> <br>

    <label><strong>Gender*</strong></label>
    <input style="margin-left: 22%;" type="radio" name="gender" value="male" >Male<br>
    <input style="margin-left: 39%;" type="radio" name="gender" value="female" >Female<br>
    <br>
     <label for="fname"><strong>Password*</strong></label>
     <input style="margin-left: 19%;"  type="password" name="password" id="password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{7,}"  title="at least one number and one uppercase and lowercase letter, and at least 7 characters" required></td>

     <br> <br>
     <label for="email"><strong>Email</strong></label>
     <input style="margin-left: 28%;" type="email" id="em" name="email" placeholder="Your MailId..."> <br> <br>

    <label for="add"><strong>Address*</strong></label> 
    <textarea style="margin-left: 21%;" id="address" name="address" style="height:50px"></textarea>
    <br> <br>

    <label for="mark1"><strong>SSLC %*</strong></label>
     <input style="margin-left: 20%;" type="text" id="sslc" name="sslc" maxlength="2"> <br> <br>

     <label for="mark2"><strong>HSC %*</strong></label>
     <input style="margin-left: 22%;" type="text" id="HSC" name="hsc" maxlength="2"> <br> <br>

    <label for="mark"><strong> CGPA* </strong></label>
     <input style="margin-left: 25%;" type="text" id="CGPA" name="cgpa" maxlength="3"> <br> <br>

   <label><strong>Proficient Languages*</strong></label> 
  <input style="margin-left: 12%;" type="checkbox" name="lang" value="C++">C and C++ <br>
  <input style="margin-left: 55%;" type="checkbox" name="lang" value="python">Python <br>
  <input style="margin-left: 55%;" type="checkbox" name="lang" value="java">Java<br>
  <input style="margin-left: 55%;" type="checkbox" name="lang" value="Php">PHP <br>
  <br>

    <label for="intern"><strong>Internships(if any)*</strong></label> 
    <textarea id="int" name="internships" style="height:50px"></textarea>
    <br> <br>

   <label for="country"><strong>Preferred location</strong></label>
    <select id="country" name="preferred_loc">
      <option value="chennai">Chennai</option>
      <option value="bombay">Bombay</option>
      <option value="bang">Bangalore</option>
      <option value="hyd">Hyderabad</option>
    </select> <br> <br>
    <br>
   <input type="submit" value="SUBMIT"> 
   <input style="margin-left: 10%;" type="reset" value="Reset">
    <a style="margin-left: 10%;" href=http://localhost:3000>BACK</a>
     </form>


</div>

我的.pm文件的内容:

package ors;
use Dancer ':syntax';
use Dancer::Plugin::Database;
use File::Spec;
use File::Slurp;
use Template;

set 'database'     => File::Spec->catfile(File::Spec->tmpdir(), 'campus.db');
set 'session'      => 'Simple';
set 'template'     => 'template_toolkit';
set 'logger'       => 'console';
set 'log'          => 'debug';
set 'show_errors'  => 1;
set 'startup_info' => 1;
set 'warnings'     => 1;
set 'username'     => 'admin';
set 'password'     => '';
set 'layout'       => 'main';

our $VERSION = '0.1';

get '/' => sub {
    template 'index';
};

get '/admin' => sub {
    template 'admin';
}; 

get '/student' => sub {
    template 'student';
};

get '/company' => sub {
    template 'company';
};

get '/appform' => sub {
    template 'appform';
};

my $flash;

sub set_flash {
       my $message = shift;

       $flash = $message;
}

sub get_flash {

       my $msg = $flash;
       $flash = "";

       return $msg;
}

sub connect_db {
       my $dbh = DBI->connect("dbi:SQLite:dbname=".setting('database')) or
               die $DBI::errstr;

       return $dbh;
}

sub init_db {
       my $db = connect_db();
       my $schema = read_file('./schema.sql');
       $db->do($schema) or die $db->errstr;
}

post '/appform' => sub {

        my $db = connect_db();
        my $sql = 'insert into student (name,mobile_no,gender,email,address,sslc,hsc,cgpa,languages,internships,preferred_loc,password,applied_job,company_applied) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
        my $sth = $db->prepare($sql); 
        $sth -> execute(params->{'name'},params->{'mobile_no'},params->{'gender'},params->{'email'},params->{'address'},params->{'sslc'},params->{'hsc'},params->{'cgpa'},params->{'languages'},params->{'internships'},params->{'preferred_loc'},params->{'password'}) or die $sth->errstr;
        #$sth->execute();   
        set_flash('New entry posted!');
            redirect '/';
    };

any ['get', 'post'] => '/student' => sub {

       my $err;
       my $db = connect_db();
       my $sql = 'select name and password from student WHERE name=?  AND password=?';
       my $sth = $db->prepare($sql) or die $db->errstr;
       $sth->execute();

       if ( request->method() eq "POST" ) {

       if ( params->{'name'} ne setting('name') ) {
                       $err = "Invalid username";
        }
       elsif ( params->{'password'} ne setting('password') ) {
                       $err = "Invalid password";
        }
       else {
              session 'logged_in' => true;
              set_flash('You are logged in.');
              return redirect '/';
            }
       }

       template 'add.tt', {
               'err' => $err,
       }
    };

any ['get', 'post'] => '/company' => sub {
       my $err;
       my $db = connect_db();
       my $sql = 'select name and password from company WHERE name=?  AND password=?';
       my $sth = $db->prepare($sql) or die $db->errstr;
       $sth->execute() or die $db->errstr;

       if ( request->method() eq "POST" ) {

       if ( params->{'name'} ne setting('name') ) {
                       $err = "Invalid username";
        }
       elsif ( params->{'password'} ne setting('password') ) {
                       $err = "Invalid password";
        }
       else {
              session 'logged_in' => true;
              set_flash('You are logged in.');
              return redirect '/';
            }
        }


       template 'add.tt', {
               'err' => $err,
       }
    };

any ['get', 'post'] => '/admin' => sub {
       my $err;
       my $db = connect_db();
       my $sql = 'select name and password from admin WHERE name=?  AND password=?';
       my $sth = $db->prepare($sql) or die $db->errstr;
       $sth->execute() or die $db->errstr;

       if ( request->method() eq "POST" ) {

       if ( params->{'name'} ne setting('name') ) {
                       $err = "Invalid username";
        }
       elsif ( params->{'password'} ne setting('password') ) {
                       $err = "Invalid password";
        }
       else {
              session 'logged_in' => true;
              set_flash('You are logged in.');
              return redirect '/';
            }
        }

       template 'add.tt', {
               'err' => $err,
       }  


};





true;

问题是当我尝试填写申请表并在数据库中输入值时,它显示错误: Screenshot of the error

2 个答案:

答案 0 :(得分:4)

my $sth = $db->prepare($sql);

$sth未定义,因为prepare失败,但是您忽略了检查错误。请参阅documentation for prepare

要在所有位置简单地启用错误检查,请在构造函数中设置RaiseError

DBI->connect("dbi:SQLite:dbname=".setting('database'), undef, undef, {RaiseError => 1}) …

答案 1 :(得分:1)

欢迎来到该网站。正如Haem所提到的,发布问题时最好使用文本而不是图像。尽管如此,也许下面的信息仍然有用...

SQL占位符和绑定参数

您的查询可能失败,因为您绑定的参数数量不等于SQL占位符数量。您有14个占位符-但是在execute(...)语句中仅绑定12个参数。

您的/admin/student/company路由处理程序中存在相同的问题-在每种情况下,您都要准备带有两个占位符的语句-例如

my $sql = 'select name and password from admin WHERE name=?  AND password=?';
my $sth = $db->prepare($sql);

,但是尝试在不绑定任何参数的情况下执行它:

$sth->execute();

如果在查询中使用占位符,则必须使用$sth->bind_param(...)绑定参数或将值传递到$sth->execute(...)

Dancer的数据库插件

由于您使用的是Dancer的'Plugin :: Database'模块,因此无需在每个请求上都建立新的数据库连接。相反,该模块可以自动为您连接。

您只需要首先在config.yml中定义相关的数据库参数。

也就是说,在您的config.yml中,添加以下内容(因为您使用的是SQLite):

plugins:
    Database:
        driver: SQLite
        database: 'campus.db'

然后,替换

my $db = connect_db();
my $sth = $db->prepare($sql);

使用

my $sth = database->prepare($sql);

完成此操作后,您就可以完全删除db_connect()函数。

但是必须在配置文件中预先将数据库参数设置为set correctly