简体   繁体   English

Perl舞者错误执行方法未定义

[英]Perl dancer error execute method undefined

I was trying to make a website using perl dancer, below is my code. 我试图使用Perl舞者制作一个网站,下面是我的代码。 It seems to be correct but the page keeps loading and never enters the values in the database. 看来是正确的,但是页面会继续加载,并且永远不会在数据库中输入值。 When I cancel the page I get an error stating " request to POST /appform crashed: Can't call method "execute" on an undefined value ". 当我取消页面时,出现错误,指出“ request to POST /appform crashed: Can't call method "execute" on an undefined value ”。 I can't figured out whats wrong in the code. 我无法弄清楚代码中有什么问题。 If you have any other code please mention. 如果您还有其他代码,请提及。 I am using SQLite for database. 我正在使用SQLite的数据库。 There is a database campus.db and I am inserting the value in student table. 有一个campus.db数据库,我正在将值插入到student表中。

post '/appform' => sub {

        my $q = CGI ->new;
        my $name = $q->param ("firstname");
        my $password = $q->param("password");
        my $mobile_no = $q->param("mobile");
        my $gender = $q->param("gender");
        my $email = $q->param("email");
        my $address = $q->param("address");
        my $sslc = $q->param("SSLC");
        my $hsc = $q->param("HSC");
        my $cgpa = $q->param("cgpa");
        my $languages = $q->param("lang");
        my $internships = $q->param("intern");
        my $preferred_loc = $q->param("country");
        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 = database->prepare($sql); 
        $sth->execute($name,$mobile_no,$gender,$email,$address,$sslc,$hsc,$cgpa,$languages,$internships,$preferred_loc,$password) or die $sth->errstr;
        #$sth->execute();   
        $sth-> finish;
        set_flash('New entry posted!');
            redirect '/';
    };

You're using the database keyword to get a database handle. 您正在使用database关键字获取数据库句柄。 I'm guessing that's coming from Dancer2::Plugin::Database (it would be useful if you could include information like this in your question). 我猜想这是来自Dancer2 :: Plugin :: Database的 (如果您可以在问题中包含这样的信息,这将很有用)。

The error says that you're calling execute() on an undefined value. 该错误表明您正在对未定义的值调用execute() You're calling execute() on the variable $sth . 您正在对变量$sth调用execute() So $sth is undefined. 因此$sth是未定义的。 You get $sth by calling prepare() on the database handle returned from database() . 通过在database()返回的数据库句柄上调用prepare()可以得到$sth So it looks like the prepare() call is failing. 因此,似乎prepare()调用失败。 You should check the return value from that call and throw an error if it fails. 您应该检查该调用的返回值,如果失败则抛出错误。

The most common reason for prepare() to fail is that you're trying to compile an SQL statement that contains an error. prepare()失败的最常见原因是您正在尝试编译包含错误的SQL语句。 I can't see any obvious error in your SQL, but it's worth checking it by running it manually against your database. 我在您的SQL中看不到任何明显的错误,但是值得通过在数据库上手动运行来检查它。

I see you're using bind params in your SQL statement. 我看到您在SQL语句中使用绑定参数。 That's a great idea, but please note that you don't need to quote the question marks in your SQL - the database driver will handle that for you. 这是个好主意,但是请注意,您不需要在SQL中引用问号-数据库驱动程序将为您处理。 I don't think that's what is causing your problem though. 我不认为这是造成您问题的原因。

I also see that you're using CGI.pm inside your Dancer app to get the request parameters. 我还看到您在Dancer应用程序中使用CGI.pm来获取请求参数。 To be honest, I'm slightly surprised that it works - but it's a terrible idea. 老实说,我对此感到有些惊讶-但这是一个糟糕的主意。 Dancer has its own keywords that will give you this information. 舞者有其自己的关键字,它将为您提供此信息。 Look at query_parameters() , body_parameters() and route_parameters() in the Dancer documentation . 查看Dancer文档中的query_parameters()body_parameters()route_parameters()

除了已经提出的要点之外,您的DBI prepare()调用可能还失败了(添加错误检查以查看原因,例如, my $sth = database->prepare('...') or die "DB error: " . database->errstr ),并且您使用的是舞者应用程序内CGI.pm(......不这样做,我很惊讶,它会在所有的工作-看舞者文档如何访问PARAMS您的应用已发送),还请查看Dancer :: Plugin :: Database / Dancer2 :: Plugin :: Database提供的quick_insert便捷方法,因此您根本不必编写该SQL INSERT语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM