Perl CGI将空值发送到mySQL数据库?

贾斯·刘易斯

场景:我有一个HTML表单,该表单将变量发送到Perl CGI,然后将其接收并将其插入到我之前创建的SQL DB中,但是问题是它仅向该DB发送NULL值-它确实发送了“正确的数字”为null,因此我不知道出了什么问题。我觉得这与将变量传递给Perl而不是将Perl传递给DB有关。Perl文件:

  #! \xampp\perl\bin\perl.exe -w

require "dbfunc.pl";

use warnings;
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);


$table  = "routes";
#$spotted = "spotted";
$booked = "bookings";
$logged = "log";

$dbh = getConnection();




print header;
print start_html("Journey Details");

$name = param($name);
$email = param($email);
$price = param($price);
$date = param($date);
$departure = param($departure);
$arrival = param($arrival);
$adults = param($adults);
$children = param($children);
$totalCost = param($totalCost);
$departureTime = param($departureTime);
$arrivalTime = param($arrivalTime);
$jid = param($jid);



    $dbh->do("INSERT INTO $logged VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", undef,
           $date, $date, $name, $email, $departure, $arrival, $departureTime, $adults, $children, $totalCost);

    #my $sth = $dbh->prepare(qq{INSERT INTO $logged SET DateBooked=?, Journeydate=?, Name=?, Email=?, RouteFrom=?, RouteTo=? , DepartTime=?, Adults=?, Children=?, AmountPaid=?});
    #$sth->execute($date, $date, $name, $email, $departure, $arrival, $departureTime, $adults, $children, $totalCost) or die $dbh->errstr;


print end_html;

最初使用vars的第一个perl文件:

#! \xampp\perl\bin\perl.exe -w

use CGI qw(:standard);
$query = new CGI;
@parameters = $query -> param;

print header, start_html("Receipt");

    print p("Your Journey Receipt");

    my $name = $query->param('name');
    print ("Name: $name");

    print br;

    my $email = $query->param('email');
    print ("Email: $email");

    print br;

    my $price = $query->param('price');
    print ("Price: &pound$price");

    print br;

    my $date = $query->param('date');
    print ("Journey date: $date");

    print br;

    my $departure = $query->param('departure');
    print ("From: $departure");

    print br;

    my $arrival = $query->param('arrival');
    print ("To: $arrival");

    print br;

    my $adults = $query->param('adults');
    print ("Adults: $adults");

    print br;

    my $children = $query->param('children');
    print ("Children: $children");

    print br;

    my $totalCost = $query->param('totalCost');
    print ("Total Cost:  &pound$totalCost");

    print br;

    my $departureTime = $query->param('departureTime');
    print ("Departure: $departureTime");

    print br;

    my $arrivalTime = $query->param('arrivalTime');
    print ("Arrival: $arrivalTime");

    print br;

    my $jid = $query->param('jid');
    print ("Journey ID: $jid");

    print br;

    print qq!<br><form><input type="Button" value="Back" onclick="history.back()"></form>!;
    print qq!<br><form method="get" action="serverside.pl">!;
    print qq!<input type="submit" value="Confirm Booking" />\n</form><br />!;

print end_html;
昆汀

您多次犯同样的错误。我将以第一个实例为例:

$name = param($name);

您获得$name(尚未定义)的值,并使用它从HTTP请求中获取一个参数。由于未定义,因此您不会获得所需的结果,因此也不会在中获得提交的数据$name

大概您打算:

$name = param('name');

立即更新,您拥有使用的表格:

print qq!<br><form method="get" action="serverside.pl">!;
print qq!<input type="submit" value="Confirm Booking" />\n</form><br />!;

<input>除了“提交”按钮(没有name属性)之外,您没有任何其他元素,因此没有要提交的数据。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章