我使用DD / MM / YYYY格式的html表单输入日期。我想将它在perl中转换为YYYY-MM-DD,以便我可以将其输入MySQL数据库。
我不确定如何做到这一点,任何帮助将不胜感激。
我尝试过这样做,但是当我输入记录时,日期显示在我的数据库中的0000-00-00
以下是我的PERL代码:
#!/xampp/perl/bin/perl -w
require "dbfunc.pl";
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);
$dbh = getConnection();
$Btable = "booking";
$CDtable = "carddetails";
$Stable = "seats";
$Rtable = "route";
$CustomerID = param("CustomerID");
$Rid = param("Rid");
my $Tdate = param("Tdate");
$Tdate =~ s/(.{2})(.{2})(.{4})/\3\2\1/;
$Aticket = param("Aticket");
$Cticket = param("Cticket");
$tickettotal = $Aticket + $Cticket;
$Cname = param("Cname");
$Ctype= param("Ctype");
$Noc = param("Noc");
$Scode = param("Scode");
$Edate = param("Edate");
#Booking Refrence Generator
my @chars = ("A".."Z", "a".."z", "1".."9");
my $BookingReference;
$BookingReference .= $chars[rand @chars] for 1..10;
sub selectTable {
$statement = shift (@_);
## select data from a table and output as an HTML table
## Prepare and Execute an SQL select statement
$sth = $dbh->prepare($statement);
$sth->execute();
## Get the data as an array of arrays and output by position
while (@ary = $sth->fetchrow_array) {
if ($Rid == 1) {
$tot1 = $Aticket * $ary[5];
$tot2 = $Cticket * $ary[6];
}
if ($Aticket == 0){
$tot2 = $Cticket * $ary[5]
}
if ($Rid == 2){
$tot1 = $Aticket * $ary[5];
$tot2 = $Cticket * $ary[6];
}
if ($Aticket == 0){
$tot2 = $Cticket * $ary[5]
}
if ($Rid == 3){
$tot1 = $Aticket * $ary[5];
$tot2 = $Cticket * $ary[6];
}
if ($Aticket == 0){
$tot2 = $Cticket * $ary[5]
}
if ($Rid == 4){
$tot1 = $Aticket * $ary[5];
$tot2 = $Cticket * $ary[6];
}
if ($Aticket == 0){
$tot2 = $Cticket * $ary[5]
}
if ($Rid == 5){
$tot1 = $Aticket * $ary[5];
$tot2 = $Cticket * $ary[6];
}
if ($Aticket == 0){
$tot2 = $Cticket * $ary[5]
}
if ($Rid == 6){
$tot1 = $Aticket * $ary[5];
$tot2 = $Cticket * $ary[6];
}
if ($Aticket == 0){
$tot2 = $Cticket * $ary[5]
}
if ($Rid == 7){
$tot1 = $Aticket * $ary[5];
$tot2 = $Cticket * $ary[6];
}
if ($Aticket == 0){
$tot2 = $Cticket * $ary[5]
}
if ($Rid == 8){
$tot1 = $Aticket * $ary[5];
$tot2 = $Cticket * $ary[6];
}
if ($Aticket == 0){
$tot2 = $Cticket * $ary[5]
}
if ($Rid == 9){
$tot1 = $Aticket * $ary[5];
$tot2 = $Cticket * $ary[6];
}
if ($Aticket == 0){
$tot2 = $Cticket * $ary[5]
}
if ($Rid == 10){
$tot1 = $Aticket * $ary[5];
$tot2 = $Cticket * $ary[6];
}
if ($Aticket == 0){
$tot2 = $Cticket * $ary[5]
}`enter code here`
if ($Rid == 11){
$tot1 = $Aticket * $ary[5];
$tot2 = $Cticket * $ary[6];
}
if ($Aticket == 0){
$tot2 = $Cticket * $ary[5]
}
print qq!
<td>$ary[4]</td>
</tr>\n!;
}
print "</table>\n";
}
sub insertTable {
$statement = shift (@_);
$dbh->do($statement);
} # sub
print header;
print start_html("Booking Information");
print qq!
<link rel="stylesheet" href="styles.css">
<div id="cssmenu">
<ul>
<li> <a href="http://localhost/webprog/Home.pl"><span>Home</span></a></li>
<li class="active"><a href="http://localhost/webprog/RouteBooking.pl"><span>Booking</span></a></li>
<li><a href="http://localhost/webprog/liveroute.pl"><span>Timetables</span></a></li>
<li class="last"><a href="http://localhost/webprog/ContactUs.pl"><span>Contact Us</span></a></li>
</ul>
</div>
<div id="Titlebar">
<header> Route Availbility</header>
</div>
<div id="Sidebar"> </div>
!;
selectTable("SELECT * FROM $Rtable WHERE RouteID='".$Rid."'");
#selectTable("SELECT * FROM $Stable WHERE RouteID='".$Rid."' AND Date='".$Tdate."'");
$dbh->do("INSERT INTO $Btable(CustomerID,BookingReference,Date,RouteID,TotalPassengersBooked,AdultTickets, ChildTickets) VALUES ('".$CustomerID."', '".$BookingReference."', '".$Tdate."' , '".$Rid."', '".$tickettotal."', '".$Aticket."', '".$Cticket."')");
$dbh->do("INSERT INTO $CDtable(CustomerID,NameOnCard,CardType,NumberOnCard,SecruityCode,Expirydate) VALUES ('".$CustomerID."', '".$Cname."','".$Ctype."','".$Noc."','".$Scode."','".$Edate."')");
$Tdate =~ s/-//gi;
$dateandrid = $Tdate . $Rid;
$dbh->do("INSERT INTO $Stable(RouteAndDate,Date,RouteID) VALUES ('$dateandrid','$Tdate','$Rid') ON DUPLICATE KEY UPDATE Seats=Seats");
$dbh->do("INSERT INTO $Stable(RouteAndDate,Date,RouteID) VALUES ('$dateandrid','$Tdate','$Rid') ON DUPLICATE KEY UPDATE Seats=Seats-'$tickettotal'");
print qq! <p>Your booking details are: <br>
<br> <p>Membership ID: $CustomerID <br>
<br> <p>Route ID: $Rid <br>
<br> <p>Travel Date: $Tdate : YYYY-MM-DD <BR>
<br> <p>Total tickets purchased: $tickettotal <br>
<br> <p>Consisting off $Aticket adult tickets and $Cticket child tickets <br>!;
$total = $tot1 + $tot2;
print qq! Total Price is $total pounds <br>!;
print qq!
<br>Please make note of your Booking Refrence as it is required to cancel your booking
<br>
<br> Your Booking Reference Is - $BookingReference <br>
<br> Right click Download File and select save file as to download
<br> <br>
!;
print qq! <a href="report.txt">Download File</a>!;
my $filename = 'report.txt';
open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
print $fh "Membership ID: $CustomerID\n";
print $fh "RouteID: $Rid\n";
print $fh "Travel Date: $Tdate\n";
print $fh "Total Tickets Purchased: $tickettotal\n";
print $fh "Consisting off $Aticket adult tickets and $Cticket child tickets\n";
print $fh "Total price is $total pounds\n";
print $fh "Please make note of your booking reference as it required to cancel your booking if you wish to do so\n";
print $fh "\n";
print $fh "Your Booking Reference is: $BookingReference";
close $fh;
PERL文件中的HTML CODE:
#!/xampp/perl/bin/perl -w
require "dbfunc.pl";
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);
print header;
print start_html("Route Booking");
print_html_head_section();
print qq!
<link rel="stylesheet" href="styles.css">
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="script.js"></script>
<title>Home</title>
</head>
<body>
<div id="cssmenu">
<ul>
<li><a href="http://localhost/webprog/Home.pl"><span>Home</span></a></li>
<li class="active"><a href="http://localhost/webprog/RouteBooking.pl"><span>Booking</span></a></li>
<li><a href="http://localhost/webprog/liveroute.pl"><span>Timetables</span></a></li>
<li><a href="http://localhost/webprog/ContactUs.pl"><span>Contact Us</span></a></li>
<li class="last"><a href="http://localhost/webprog/Login.html"><span>Employee Login</span></a></li>
</ul>
</div>
<div id="Titlebar">
<header> Booking</header>
</div>
<div id="Sidebar"> <img src="logo.jpg" alt="WebBus" style=width:225px;height:225px></div>
!;
print "<h1> Route Selection</h1>";
print qq!<form method="POST" name="MyForm" onsubmit="return f2()" action="RouteBookingInsert.pl"><br />!;
print qq!<p> CustomerID: <input type="text", name="CustomerID", id="CustomerID" size=20><br>! ;
print qq! Select Route ID: <select name="Rid">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
</select><br>!;
print qq! Date of travel: <input type="text", id="Tdate" , name="Tdate" size=20> <br>! ;
print qq! Number of Adult Tickets: <select name="Aticket">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select><br>!;
print qq! Number of Child Tickets: <select name="Cticket">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select><br>!;
sub someJS {
print "<button type='button' onclick='return f2()'>Try it</button> \n";
}
sub print_html_head_section {
print "<head>\n";
print "<script src='RouteBookingValidation.js' type='text/javascript'></script>\n";
print "</head>\n";
}
print qq! <br>!;
print "<h1>Card Details</h1>\n";
print qq!<p> Name On Card: <input type="text", id="Cname" , name="Cname" size=20> <br> !;
print qq! Card Type: <select name="Ctype">
<option value="Visa">Visa</option>
<option value="Mastercard">Mastercard</option>
<option value="Amex">Amex</option>
<option value="Solo">Solo</option>
</select><br>!;
print qq! Number On Card:<input type="text", id="Noc" , name="Noc" size=20> <br>! ;
print qq! Secruity Code: <input type="text", id="Scode" , name="Scode" size=20> <br>! ;
print qq! Expiry Date: <input type="text", name="Edate" , id="Edate" size=20> <br>! ;
print qq!<br> !;
print qq!<br /><input type="submit" value="Book" style="width:50px"/>\n</form><br />!;
print end_html;
由于
答案 0 :(得分:3)
您脚本中的代码:
my $Tdate = param("Tdate");
$Tdate =~ s/(.{2})(.{2})(.{4})/\3\2\1/;
将转向:
18/04/2015
成:
4/20/01815
这可能不是你想要的,MySQL应该完全拒绝,而是将它存储为空日期。
你可能想要这样的东西:
$Tdate =~ s{^(\d{2})/(\d{2})/(\d{4})$}{$3-$2-$1};
如果用户以您期望的格式提供日期,那将会有效。您确实应该添加一些验证逻辑来确认提交的日期是有效格式并且具有日,月和年的有效值。请考虑使用DateTime或DateManip等模块。
同样在网页中,您可能会考虑使用类似date entry widget的内容。
答案 1 :(得分:0)
当尝试从CSV解析数据并使用Perl将其插入Postgres SQL时,我遇到了同样的问题。我来添加了这段代码,所有代码对我都有效
my $date = '31/01/2019';
$date =~ m /^(\d{2})\/(\d{2})\/(\d{4})$/;
my $fdate = "$3-$2-$1";
print $fdate . "\n";
这个单线也可以做
$date = '31/01/2019';
$date =~ s /(^\d{2})\/(\d{2})\/(\d{4}$)/$3-$2-$1/;
希望会有所帮助。