使用perl在MySql DB中插入表单数据

时间:2013-05-27 04:07:31

标签: html mysql perl cgi

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

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

HTML code ...

     <!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;;

对于复选框值,我只是想在DB中输入“check1”和“check2”值,这就是为什么我没有为它们提取参数..

表结构..

enter image description here

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

错误讯息。

enter image description here

错误消息的第二部分 enter image description here

错误消息的第3部分 enter image description here

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

由于编译器说你有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(?,?,?,?,?,?,?,?,?,?,?,?,?,?)

我认为这会对你有帮助..