CGI::应用程序和 SQLite

发布于 2024-12-11 09:12:35 字数 873 浏览 0 评论 0原文

过去几天我一直在摆弄 CGI::application 并决定创建一个非常基本的论坛:第一页显示所有帖子(仅第一级,没有回复或任何内容)和一个可用于创建的表单一个新帖子。

我遇到的问题是输入表单的数据永远不会插入到 SQLite 数据库中。

这是我遇到问题的子过程:

sub newpost {

my $self = shift;


if ( $self->param() ){

    my $dbh = DBI->connect("dbi:SQLite:dbname=$database_file","","");



    my $sth = $dbh->prepare("INSERT INTO posts (author, time, text)        VALUES('testuser', '2011-10-23', 'This is a test!')");


    $sth->execute();

    $self->header_type('redirect');
    $self->header_props(-url=> '?rm=viewall');

}

else {

    my $tmpl_obj = $self->load_tmpl('newpost.html');

    return $tmpl_obj->output();

}

正确发生的情况是,当首次调用 newpost 运行模式时,将运行 else 语句中的代码(加载带有表单的模板)。表单的操作调用相同的运行模式,但现在提供了参数,将运行 if 语句中的代码。我已经检查了 SQL 代码本身并且它可以工作,所以肯定还有其他东西我忽略了。

另外,以这种方式实现表单逻辑是否被认为是最佳实践?

谢谢

I've been messing around with CGI::application the past couple of days and decided to create a really basic forum: the first page displays all posts (only first level, no replies or anything) and a form which can be used to create a new post.

The issue I'm running into is that the data that gets entered into the form never gets inserted into the SQLite database.

Here's the sub procedure I'm having trouble with:

sub newpost {

my $self = shift;


if ( $self->param() ){

    my $dbh = DBI->connect("dbi:SQLite:dbname=$database_file","","");



    my $sth = $dbh->prepare("INSERT INTO posts (author, time, text)        VALUES('testuser', '2011-10-23', 'This is a test!')");


    $sth->execute();

    $self->header_type('redirect');
    $self->header_props(-url=> '?rm=viewall');

}

else {

    my $tmpl_obj = $self->load_tmpl('newpost.html');

    return $tmpl_obj->output();

}

What happens correctly is that when the newpost run mode is first called, the code within the else statement is run (the template with the form is loaded). The action for the form calls this same run mode, but now that parameters are being provided, the code in the if statement is run. I've checked the SQL code itself and it works, so there must be something else I'm over looking.

Also, is it considered best practice to go about implementing the form logic in this way?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

固执像三岁 2024-12-18 09:12:35

您将 $self->param()$self->query->param 混淆了。第一个是每个请求的应用程序级别参数(您可以在一种方法中设置并在另一种方法中再次使用的东西),第二个是来自 GET 查询字符串或请求的 POST 正文的参数。如果您期望用户提供某些信息,则该信息将位于 $self->query->param 中。

顺便说一句,$self->query 对象是一个普通的 CGI 对象,因此请参阅它的文档以了解具体信息。

You're confusing $self->param() with $self->query->param. The 1st is per-request application level parameters (stuff you might set in one method and use again in another method) and the 2nd are the parameters from the GET query string or the POST body of the request. If you're expecting something from the user it will be in $self->query->param.

BTW, the $self->query object is a normal CGI object, so see it's documentation for specifics.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文