【问题标题】:Insert form data in MySql DB using perl使用 perl 在 MySql DB 中插入表单数据
【发布时间】:2013-05-21 21:29:48
【问题描述】:

我想使用 perl 在 mysql 数据库中插入表单数据。为此我制作了 2 个文件,第一个是 html 文件,它接受输入,第二个是 .cgi 文件,由 html 文件调用以将数据存储在数据库中使用插入查询。

我面临的问题是我能够从用户那里获取输入,但由于 cgi 文件无法从 html 文件中获取值,因此无法在 DB 中插入值。我也得到了表单提交上的语法错误消息..这两个相关吗??

HTML 代码...

     <!DOCTYPE html>
     <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title> Report</title>
    <h1 style="text-align:center;color:white;background-color:blue" Reports</h1>
        <style>

        .label 
            { 
                float: left; 
                width:120px;
                padding:10px 30px;
                <!--font-weight:bold;-->
            }
        textarea
            {
                <!--margin-bottom:90px;
                margin-top:50px";-->
                vertical-align:top;
                overflow : auto;                    
            }
        .textarea
            {
                resize :none;
                border: none;
                width: 100%;
                -webkit-box-sizing: border-box;   <!-- <=iOS4, <= Android  2.3 */-->
                -moz-box-sizing: border-box; <!-- FF1+ -->
                -box-sizing: border-box; <!-- Chrome, IE8, Opera, Safari 5.1-->
            }
            table.table, th.table, td.table
            {
                border: 1px solid black;
                border-collapse: collapse;
                padding: 2px;
            }
            div#box
            {
                border:1px solid black;
                align:center;
                width:250px;
                height:250px;
                padding:10px;
            }
            span#sub
            { 
                display:block; 
                font-weight:bold;
                text-decoration:underline;
                text-align:center;
            }
            label#main 
            {
                <!--display:block;-->
                font-weight:normal;
                margin-bottom:10px;
            }
        textarea#strat 
            {
                border:none;
                width:100%;
                height:65%;
                resize:none;

            }

        </style>
        </head>

<body>
<form action="jaihoshanidevki.cgi" method="post">

<label for="name" class="label">Name</label>
<textarea id="name" rows="1px" cols="20px" style="resize:none" placeholder="Name">sample name</textarea>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<label for="desig" >Designation:</label>
<textarea id="desig" rows="1px" cols="20px" style="resize:none" placeholder="Designation">designation</textarea><br><br><br>

<label for="dept" class="label">Department</label>
<textarea id="dept" rows="2px" cols="20px" style="resize:none" placeholder="Function/Department"> department</textarea>
&nbsp;&nbsp;&nbsp;&nbsp;

<label for="date1">Date(YYYY/MM/DD):</label>
<textarea id="date1" rows="1px" cols="14px" style="resize:none" placeholder="Date">2013/05/22</textarea><br><br><br>

<label for="cname" class="label"> Cluster Name</label>
<textarea id="cname" rows="1px" cols="20px" style="resize:none" placeholder="Cluster Name">risk cluster number 1</textarea>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<label for="cnum"> Cluster Number:</label>
<textarea id="cmun" rows="1px" cols="12px" style="resize:none" placeholder="Cluster Number">sample cluster number</textarea><br><br><br>



<p><b>Identification Methodology: (Tick the appropriate option)</b></p>

<input type="checkbox" id="roa"> Already Occured                                   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" id="roc"> industry:<br><br>              


<label for="date2" class="label">Date of Occurance(DD/MM/YY):</label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<textarea id="date2" rows="1" cols="10" style="resize:none" placeholder="Date">2013/05/22</textarea><br><br><br>

<label for="other" class="label">Others:(Please specify)</label>&nbsp;&nbsp;
<textarea id="other" rows="2" cols="30" style="resize:none" placeholder="others">sample other</textarea><br><br><br>



<p><b>Preliminary Details(briefly):</b></p>

<label for="desc" class="label">Description</label>
<textarea id="desc" rows="5" cols="50" style="resize:none" placeholder="Risk Description">sample desc</textarea><br><br>

<label for="root" class="label">Root</label> 
<textarea id="root" rows="5" cols="50" style="resize:none" placeholder="Cause">sample root</textarea><br><br>

<label for="rqi" class="label">DQI</label>
<textarea id="rqi" rows="5" cols="50" style="resize:none" placeholder="Description and Qualification">sample DQI</textarea><br><br>



<p><b> Category (Tick the appropriate option) :</b></p> 

<input type="checkbox" id="stra">Strategic                           
 &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="roc">Operational                           
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="repu">Reputational

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><br>
<input type="checkbox" name="comp">Compliance
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <br><br><br>

<div id="box" align="center">
<label for="strat" id="main"><span id="sub" style="text-align:center">Preliminary Risk Mitigation Strategy:</span>(Report in brief, responsibility & next review date)</label> 
<textarea id="strat" rows="5" cols="20" placeholder="Enter Data" >sample strategy</textarea>
 </div><br><br>

 <input type="submit" id="submit">  <br><br>

                    </form>
                </body>
                </html>

.cgi 文件..

  #!/usr/sbin/perl
  print "Content-type: text/html\n\n";
  use DBI;
  use CGI;
  use CGI::Carp qw (fatalsToBrowser);
  $dbh = DBI->connect("DBI:mysql:database=Demo;host=localhost","mint", "MINT123", {'RaiseError' => 1});
  $q = CGI->new;
  $query=CGI->new;
  $name=$query->param('name');
  $desig=$query->param('desig');
  $dept=$query->param('dept');
  $date1=$query->param('date1');
  $cname=$query->param('cname');
  $cnum=$query->param('cnum');
  #$im=$query->param('name');
  $date2=$query->param('date2');
  $other=$query->param('other');
  $desc=$query->param('desc');
  $root=$query->param('root');
  $dqi=$query->param('dqi');
  #$rc=$query->param('name');
  $strat=$query->param('strat');

  #print "$name=====uuuuuu";

 print  $q->header;
 # my $dsn = "DBI:mysql:Demo:localhost";   # Data source name
 # my $username = "mint";                 # User name
 # my $password = "MINT123";              # Password
 # my $dbh,$sth;                          # Database and statement handles
 # $dbh = DBI->connect($dsn, $username, $password);

     $sth = $dbh->prepare("Insert into FIR  (name,desig,dept,date1,cname,cnum,im,date2,other,desc,root,dqi,rc,strat) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
     $sth->execute($name,$desig,$dept,$date1,$cname,$cnum,check1,$date2,$other,$desc,
     $root,$dqi,check2,$strat);


                            $sth->finish() or die $DBI::errstr;; 
                            #print "hello";
                            $dbh->disconnect() or die $DBI::errstr;;

对于复选框值,我只是试图将“check1”和“check2”值放入数据库中,这就是我不为它们获取参数的原因..

表结构..

表是空的,它不包含任何数据..

错误信息。

错误信息的第二部分

错误信息的第三部分

【问题讨论】:

  • 您是否缺少围绕“check1”和“check2”的字符串分隔符?或者这是一个粘贴格式问题?您必须在它们周围加上引号...`$sth->execute($name,$design,$dept,$date1,$cname,$cnum,"check1",$date2,$other,$desc, $root, $dqi,"check2",$strat);'
  • 是的,我这样做了并将 desc 设置为desc...它给出了一条错误消息“列'名称'不能在 jaihoshanidevki.cgi 第 34 行为空。”...它甚至不是从表单中获取值...即使我尝试获取该特定表单值并将其存储在使用“参数”的变量中,它也没有从 html 表单中获取任何值...为什么???...我m 甚至将样本值放在每个文本区域内,以便获取值并将其存储在数据库中..

标签: html mysql perl cgi


【解决方案1】:

descreserved word in MySQL,所以你需要用反引号引用它:

$sth = $dbh->prepare("Insert into FIR (... `desc`, ...");

【讨论】:

  • 是的,我这样做了...它给出了一条错误消息“在 jaihoshanidevki.cgi 第 34 行,列'名称'不能为空。”...它甚至没有从表单中获取值.. .即使我尝试获取该特定表单值并将其存储在使用“参数”的变量中,它也没有从 html 表单中获取任何值...为什么???...我什至将示例值放入每个文本区域,以便获取值并将其存储在数据库中..
  • $name 的值是多少?是undef吗?
  • 正如您在 html 代码中看到的那样,名称的值是“示例名称”..我已经预先定义它只是为了检查我是否能够在数据库中插入值....
  • 但是你也在做$q = CGI-&gt;new; $query=CGI-&gt;new;,我不确定,但第一个CGI-&gt;new 可能正在使用传入的参数,以便第二个(你在调用param ) 可能为空。不要看你认为的输入是什么,看看它实际上是什么。并了解use strict;use warnings;my
  • 我删除了 $q = CGI->new;...在每次争论之前添加了“my”关键字并添加了“use strict;” &“使用警告;”......在这样做之后,我收到一条错误消息“无法在 jaihoshanidevki.cgi 第 36 行对未定义的值调用方法“执行”。”......知道这是什么意思吗? ??
【解决方案2】:

正如编译器说你有sql语法错误,在你的程序中,有一个语法错误。

您的查询:

Insert into FIR  (name,desig,dept,date1,cname,cnum,im,date2,other,desc,root,dqi,rc,strat) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)

表名和括号之间不能有空格。所以正确的查询是:

Insert into FIR(name,desig,dept,date1,cname,cnum,im,date2,other,desc,root,dqi,rc,strat) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)

我想这会对你有所帮助..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    相关资源
    最近更新 更多