2013-05-27 87 views
0

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

我面临的问题是,我能够从用户采取输入,但无法插入数据库中的值,因为CGI文件无法从HTML文件中取值。我也在表单提交时得到语法错误消息..这两个相关?

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” 值是这样的DB为什么我没有服用参数为他们..

表结构..

enter image description here

该表为空它不包含任何数据。

错误消息。

enter image description here

错误讯息话题的

第二部分 enter image description here

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

+0

你是否错过了'check1'和'check2'的字符串分隔符?或者这是一个粘贴格式问题? '$ sth-> execute($ name,$ desig,$ dept,$ date1,$ cname,$ cnum,“check1”,$ date2,$ other,$ desc, $ root ,$ DQI, “CHECK2”,$ STRAT);” – Josh

+0

是的,我做到了这一点,并把desc as'desc' ...它给出了一个错误消息“列名'不能为null在jaihoshanidevki.cgi第34行”......它甚至没有从形式的价值.. .even当我尝试使用特定的表单值并将其存储在使用“param”的变量中时,它没有从html表单中获取任何值...为什么??? ...我甚至在每个文本区域,以便将值存储在数据库中.. – Lucy

回答

0

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

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

是的,我做到了这一点......它给出了一个错误消息“列名'不能为null在jaihoshanidevki.cgi第34行”......它不是从表单中取值...即使当我尝试使用特定的表单值并将其存储在使用“param”的变量中时,它也没有从html表单中获取任何值...为什么??? ...我甚至会在每个文本区域内放置示例值,以便将值存储在数据库中。 – Lucy

+0

'$ name'的值是什么?它是'undef'吗? –

+0

,你可以在html代码中看到名称的值是“样本名称”..我已经使它预先定义,只是为了检查我是否能够在数据库中插入值或不... – Lucy

0

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

查询:

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

应该有表名和bracket.so正确的查询之间没有空格:

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

我认为这将帮助你..

相关问题